Module 5 · Strings

Practice

Practice~1 min

How to practice this module

Strings drills reward input hygiene: clean, normalise, then compare. Work in order — palindrome and anagram build the two-pointer and count toolkits; prefix and strStr reuse them. You are done with Strings Practice when all five show Solved in the hub.

Problems

Stringswork them in order; difficulty ascends.

0/5

solved

  1. 1Valid PalindromeEasyTwo-pointer cleanupWatch for: Filter non-alphanumerics and normalise case before comparing; advance both ends past noise
  2. 2Valid AnagramEasyCharacter countsWatch for: Length check first; one pass to count, then compare maps — sorting hides the multiset idea
  3. 3Longest Common PrefixEasyVertical / horizontal shrinkWatch for: Empty list → empty string; trim at the first mismatch, not the first word
  4. 4Find the Index (strStr)EasyWindow match scanWatch for: Return the first valid start; a needle longer than the haystack is an immediate -1
  5. 5Reverse Words in a StringMediumSplit and rebuildWatch for: Collapse runs of spaces and trim the ends; keep exactly one space between words

Cheatsheet

StringsTwo ends, builders, and character counts — not magic regex.

Smell → pattern

  • Ignore non-alphanumeric / caseFilter + two pointers
  • Same letters, different orderAnagram counts
  • Shared stem across wordsPrefix shrink
  • Words reversed, spaces messySplit / rebuild
  • Find needle in haystackScan + match window

Patterns

Two-pointer palindrome

Core

Smell: Symmetric check after cleaning

Advance L/R while skipping noise characters. Compare lowercase forms; stop when they cross.

LR

Frequency / anagram map

Safe

Smell: Permutation of the same multiset

Count characters (array of 26 or a map). Equal counts ⇔ anagram. Prefer fixed alphabets when the problem allows.

0123

Longest common prefix

Reach

Smell: Vertical or horizontal shrink

Start from the first string and trim until every word agrees — or compare column-by-column and stop at the first mismatch.

write region

Reverse words

Core

Smell: Token order flips, spacing collapses

Identify word spans, then rebuild with a single space. Trim ends; don’t reverse characters inside words unless asked.

write region

Substring search window

Careful

Smell: First index of a pattern

Slide a candidate start; match character-by-character. Know the naive O(n·m) baseline before reaching for KMP.

LRwindow

Complexity targets

  • Clean + two pointers

    Time
    O(n)
    Space
    O(1)
    Note
    Or O(n) if you build a filtered copy
  • Anagram counts

    Time
    O(n)
    Space
    O(Σ)
    Note
    Σ = alphabet size
  • Naive strStr

    Time
    O(n·m)
    Space
    O(1)
    Note
    Acceptable for short needles

Traps

  • Immutability cost

    Repeated string concatenation in a loop is quadratic in some languages. Build with a list/array, then join once.

  • Unicode vs interview alphabet

    Problems usually mean ASCII letters/digits. Don’t invent Unicode case folding unless the statement asks.