Loading…
Loading…
Charting your lesson…
Opening your lesson…
String Manipulation and Pattern Matching
Lesson 6 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
Strings are just arrays of characters, but interviewers layer immutability, character-set tricks and substring search on top. Knowing the costs of common operations — and one linear-time search algorithm — separates a confident answer from a quadratic one.
Core idea: treat a string as an indexable char array, but remember that in many languages (Java, Python, C#) strings are immutable — every "modification" builds a new string. Naive concatenation in a loop is the number-one hidden O(n²).
Step 2/ 61 min
s + t: O(|s| + |t|) — it copies both. Building a string by += in a loop of n chars is O(n²); use a mutable builder (StringBuilder, list-then-join) for O(n).For lowercase-English problems, a size-26 integer array beats a hash map on constant factors. Anagram check, "first unique character", and character counting all reduce to: count in one pass, compare/scan in another → O(n) time, O(1) space (26 is constant).
Finding pattern P (length m) in text T (length n):
Step 3/ 61 min
Are "listen" and "silent" anagrams? Build a 26-array from "listen": l,i,s,t,e,n each +1. Subtract "silent": each returns to 0. All zero → anagram. Two O(n) passes, O(1) space. Compare this to sorting both strings — that works too but costs O(n log n).
Step 4/ 61 min
Step 5/ 61 min
+= strings in a loop — accumulate in a list/builder and join once.char - 'a' maps a lowercase letter to index 0–25; watch for uppercase, digits, spaces.Writing result += s[i] inside a loop and calling it O(n). Because immutable strings copy on every concatenation, this is O(n²). Interviewers specifically watch for it; use a StringBuilder / list join for true O(n).
Step 6/ 6· Worked examples1 min
Problem
How many times does the pattern "AABAA" occur in the text "AABAACAABAA" (overlaps allowed)? Use naive matching.
Try it on paper first.
Recall
Keep this
Usage: use KMP/Z for linear matching; O(mn) DP for string similarity.
Strings are arrays of characters, but interviews test extra ideas: immutability, character-set assumptions, and efficient substring search. Knowing when naive matching is fine and when to reach for KMP or Rabin-Karp separates strong candidates.
| Algorithm | Time | Space | Best for |
|---|---|---|---|
| Naive match | O(n·m) | O(1) | tiny inputs |
| KMP | O(n+m) | O(m) | single pattern, guaranteed |
| Rabin-Karp | O(n+m) avg | O(1) | multiple patterns / avg case |
| Build string with + | O(n²) | O(n) | avoid! |
| StringBuilder append | O(n) | O(n) | correct way |
Substring search, anagram grouping, palindrome/rotation checks, or "implement strStr". Interviewers watch for the immutability trap and whether you can name an O(n+m) matcher.
Treat strings as char arrays but respect immutability (build in O(n), not O(n²)). Naive search is O(n·m); KMP guarantees O(n+m) via the LPS array; Rabin-Karp gives O(n+m) average with a rolling hash. Frequency arrays solve anagram problems in O(n).
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.