πŸ“š Arrays & Strings

Master the fundamentals of arrays and strings - the building blocks of programming.

Quick Navigation

Arrays Basics Strings Basics Common Problems My Notes Practice

πŸ”’ Arrays Basics

What is an Array?

An array is a collection of elements stored at contiguous memory locations. Elements can be accessed directly by using an index.

Key Characteristics

Common Operations

Python Example:
# Create array
arr = [1, 2, 3, 4, 5]

# Access element
print(arr[0]) # Output: 1

# Modify element
arr[2] = 10

# Array length
print(len(arr)) # Output: 5

πŸ“ Strings Basics

What is a String?

A string is a sequence of characters. In many programming languages, strings are immutable, meaning they cannot be changed after creation.

Key Characteristics

Common Operations

JavaScript Example:
// Create string
let str = "Hello World";

// Access character
console.log(str[0]); // Output: H

// String length
console.log(str.length); // Output: 11

// String methods
console.log(str.toUpperCase()); // Output: HELLO WORLD
console.log(str.indexOf("World")); // Output: 6

List of Leetcode problems

1. Two Pointers

When to use:

A two-pointer algorithm usually requires a linear data structure, such as an array or linked list. Indicators that a problem may be solved with two pointers:

  • The input is sorted (or can be sorted) β†’ common for pair-sum, triplet problems.
  • The problem involves merging or comparing two sequences (arrays, strings, lists).
  • The problem requires removing duplicates or rearranging elements in-place.
  • The problem asks for checking symmetry (e.g., palindromes).
  • The solution can be framed as expanding/shrinking a "window" (sliding window problems).
When NOT to use:

Two pointers is not the right choice if:

  • You need to count frequencies β†’ use a hashmap or Counter instead.
  • The problem involves graphs or trees (DFS/BFS are more suitable).
  • You need subsets, permutations, or backtracking (recursion is required).
  • The input is unsorted and order matters (sorting would break the logic).
  • The problem is fundamentally divide & conquer (e.g., binary search, DP).
Example:
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1] (because nums[0] + nums[1] = 2 + 7 = 9)
Why two pointers? The array is sorted and we’re asked for a pair of values β†’ move left/right pointers toward each other until sum = target.

πŸ“Š Learning Progress

Arrays Mastery

60%

Strings Mastery

45%

Problem Solving

30%