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
Strings — work them in order; difficulty ascends.
0/5
solved
- 1Valid PalindromeEasyTwo-pointer cleanupWatch for: Filter non-alphanumerics and normalise case before comparing; advance both ends past noise
- 2Valid AnagramEasyCharacter countsWatch for: Length check first; one pass to count, then compare maps — sorting hides the multiset idea
- 3Longest Common PrefixEasyVertical / horizontal shrinkWatch for: Empty list → empty string; trim at the first mismatch, not the first word
- 4Find the Index (strStr)EasyWindow match scanWatch for: Return the first valid start; a needle longer than the haystack is an immediate -1
- 5Reverse Words in a StringMediumSplit and rebuildWatch for: Collapse runs of spaces and trim the ends; keep exactly one space between words
Cheatsheet
Strings — Two 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
CoreSmell: Symmetric check after cleaning
Advance L/R while skipping noise characters. Compare lowercase forms; stop when they cross.
Frequency / anagram map
SafeSmell: Permutation of the same multiset
Count characters (array of 26 or a map). Equal counts ⇔ anagram. Prefer fixed alphabets when the problem allows.
Longest common prefix
ReachSmell: 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.
Reverse words
CoreSmell: Token order flips, spacing collapses
Identify word spans, then rebuild with a single space. Trim ends; don’t reverse characters inside words unless asked.
Substring search window
CarefulSmell: First index of a pattern
Slide a candidate start; match character-by-character. Know the naive O(n·m) baseline before reaching for KMP.
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
| Move | Time | Space | Note |
|---|---|---|---|
| Clean + two pointers | O(n) | O(1) | Or O(n) if you build a filtered copy |
| Anagram counts | O(n) | O(Σ) | Σ = alphabet size |
| Naive strStr | O(n·m) | O(1) | 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.