Strings & Tries
String Matching
When you see it
Signal words
If the prompt uses any of these, this pattern should come to mind first.
substring searchpatternrepeated stringpalindromerolling hashKMP
Practice
Drill this pattern with an AI trainer — reply only with Hint, Next, Lost, or paste your code.
Ask yourself
- Are you searching for one string inside another?
- Do repeated prefixes or suffixes matter?
- Is O(nm) too slow for the input sizes?
Classic problem
Implement strStr (LeetCode 28) — Find the first occurrence of needle in haystack. Return the index, or -1 if not found.
Input: haystack = "ABACABABC", needle = "ABABC"
Output: 4 — the pattern first appears at index 4 (haystack[4..8] = "ABABC").
Common pitfalls
Advancing both pointers after a mismatch in KMP.
Ignoring Unicode details when the prompt is not plain ASCII.