3.8.2 · D3 · HinglishString Algorithms

Worked examplesKMP algorithm — failure function, O(n+m) — full derivation

2,524 words11 min read↑ Read in English

3.8.2 · D3 · Coding › String Algorithms › KMP algorithm — failure function, O(n+m) — full derivation


Scenario matrix

Har KMP input inhi case classes mein se kisi ek mein aata hai. Har row ek aisa tarika hai jisme machine alag behave karti hai; har class ko neeche kam se kam ek worked example mila hai.

# Case class Kya trigger karta hai Kya dekhna hai
A Generic pattern, kuch internal repetition repeat + break ka mix π mein bump phir reset
B All-same string aaaa har char equal π climb karta hai — longest chain
C No border anywhere abcd sab chars distinct π sab zero
D Degenerate / empty ( ya ) tiny input , report karne ko koi match nahi
E Overlapping matches in pattern's border ≥ 1 full-match fallback se next hit milta hai
F No match at all in pattern absent kuch report nahi, phir bhi
G Deep fallback chain (nested borders) lamba partial phir mismatch ek hi par while kai baar chalta hai
H Worst-case naive killer aaa…ab in aaaa… adversarial KMP phir bhi linear; steps gino

Intuition banana — the two-window picture

Magenta window ek prefix hai (left se chipki hui). Orange window ek suffix hai (right se chipki hui). woh sabse badi length hai jis par ye dono windows identical letters rakhti hain. Jab hum "fall back" karte hain to hum magenta window ko uske apne longest self-match tak shrink karte hain — yahi woh border-of-a-border chain hai jo parent ne derive ki thi.


Case A — generic pattern ababd

Cell hit: A (bump phir reset).


Case B — all-same aaaa (the longest chain)

Cell hit: B (monotone climb).


Case C — all-distinct abcd (all zeros)

Cell hit: C (zero-border baseline).


Case D — degenerate inputs

Cell hit: D (degenerate/edge).


Case E + G — overlapping matches with deep fallback: aabaa in aabaabaaaabaa

Cells hit: E (overlap-capable pattern) aur G (deep fallback chain).


Case F — pattern absent: abc in ababab

Cell hit: F (no match).


Case H — worst-case naive killer: aaab in aaaaaaab

Cell hit: H (worst-case adversary, phir bhi ).


Potential picture

Har +1 step ko ek se uthata hai; har while step use kam se kam ek se girta hai. Kyunki 0 se shuru hota hai aur har rise ek matched text char kharch karta hai (jinka total hai), total drop kabhi total rise se zyada nahi ho sakta. Yahi poora guarantee hai, ek aise staircase ki tarah visualise karo jo utna hi gir sakti hai jitni chadhti hai. Exactly border par kyun girti hai, yeh dekhne ke liye Borders and Periods of Strings dekho.


Yahan se aage kahan jaate hain

Recall KMP apne cousins se kaise relate karta hai
  • Z-algorithm wahi "longest match from each position" question answer karta hai, lekin rolling border ki jagah poore pattern ke prefix ke against measure karta hai.
  • Rabin-Karp exact border trick ki jagah hash use karta hai — average lekin collision risk ke saath.
  • Aho-Corasick KMP ka failure function hai jo ek saath kai patterns ke liye trie par generalise ho jaata hai.
  • Suffix Automaton aur Suffix Array text ko ek baar index karte hain taaki kai pattern queries answer ho sakein.

Active Recall

Which π array does an all-same string aaaa produce?
— the maximal climbing chain.
Which π array does an all-distinct string abcd produce?
— no border ever forms.
In aabaa search, why did trigger the while loop?
Extra as failed the b comparison at , so we fell along the border chain, reusing one a.
Worst-case aaab in aaaaaaab: how many total fallback steps?
4 — one per position , each dropping by 1, bounded by .
For an empty pattern (), what must your code do?
Special-case it — it matches at every position and the loops have nothing to compare.