← All patterns
32 / 32
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

  1. Are you searching for one string inside another?
  2. Do repeated prefixes or suffixes matter?
  3. 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").

Watch it run

Common pitfalls

Advancing both pointers after a mismatch in KMP.

Ignoring Unicode details when the prompt is not plain ASCII.