3.8.5 · D1 · HinglishString Algorithms

FoundationsBoyer-Moore — bad character, good suffix heuristics

2,373 words11 min read↑ Read in English

3.8.5 · D1 · Coding › String Algorithms › Boyer-Moore — bad character, good suffix heuristics

Pehle tum do clever "skip" rules (bad character aur good suffix) ko parent note mein samajhne se pehle, tumhe kuch symbols aur pictures par pakki pakad chahiye. Yeh page har ek cheez ko scratch se banata hai. Ise upar se neeche padho; har item apne upar wale par tika hua hai.


0. String actually hoti kya hai?

Figure — Boyer-Moore — bad character, good suffix heuristics

Figure dekho. ABCAB word ko paanch boxes mein draw kiya gaya hai. Har box ke neeche uska index hai: 0, 1, 2, 3, 4. Toh:

  • The pehla box index par hai.
  • The aakhiri box index par hai.

1. Dono players: text aur pattern

Kyunki chhota wala hai, jab bhi match possible ho. Aur pattern sirf positions se tak hi shuru ho sakta hai — uske aage, chhoti strip desk ke right edge se bahar nikal jaayegi.


2. Alignment aur shift

Figure — Boyer-Moore — bad character, good suffix heuristics

Figure mein pattern strip CCTTTTGC ko text strip par par left edge ke saath dikhaya gaya hai. Ek vertical line trace karo: ke upar baitha hai, ke upar, aur generally:


3. Matching direction — right se left compare karna

Figure — Boyer-Moore — bad character, good suffix heuristics
  • WHAT karte hain: par aur par ek finger rakho, check karo ki match hai, phir dono fingers ek box left step karo.
  • WHY karte hain: agar hume mismatch milta hai last teen boxes match karne ke baad, toh hum ab jaante hain ki text un teen pattern characters se khatam hoti hai. Woh "known suffix" exactly woh fuel hai jo big-skip rules ke liye kaam aata hai. Left-to-right compare karna (naive search ki tarah) sirf matched prefix ke baare mein batayega, jo skip rules use nahi kar sakte.
  • WHAT IT LOOKS LIKE: figure mein right side par green ticks matched boxes dikhate hain; red cross woh pehla mismatch dikhata hai jab hum left move karte hain.

4. Mismatch index aur bad character

Figure — Boyer-Moore — bad character, good suffix heuristics

Figure mein: matched suffix coloured hai, index par box crossed out hai, aur uske neeche text character circled hai aur label kiya gaya hai. Aage le jaane ke liye do facts:

  1. Matched suffix ki length hai. (Boxes count karo: se tak.)
  2. Bad character ek text character hai — yeh ke andar kahi appear ho sakta hai ya nahi bhi.

5. Slice notation aur "suffix / prefix"


6. Border — ek chunk jo prefix BHI hai aur suffix BHI


7. Last-occurrence function


8. Hum jo tools borrow karte hain: , clamping, aur "safe shift"

Yeh "har rule ek safe lower bound hai, bada lo" idea poore algorithm ka logical glue hai — jab tum parent note ka boxed final formula padho toh ise pakde raho.


9. Alphabet aur "sublinear"


Prerequisite map

String, index from 0

Text T length n, Pattern P length m

Slice notation prefix and suffix

Alignment shift s and T of s plus i

Right to left comparison

Mismatch index j and bad char c

Border prefix equals suffix

last of c function

Good suffix rule

Bad character rule

max and clamp to 1

Alphabet size and sublinear

Boyer-Moore search loop

Is page par har foundation box do skip rules mein se ek ko feed karta hai, aur dono rules final search loop ko feed karte hain — exactly wahi structure jo parent note mein hai.


Equipment checklist

Khud test karo — har line question ::: answer hai.

Agar ek string ki length hai, toh uske last box ka index kya hai?
(indices se tak run karte hain).
Jab pattern shift par hai, ke neeche kaun sa text box baitha hai?
.
Boyer-Moore characters kis direction mein compare karta hai, aur kyun?
Right se left, taaki ek mismatch ek matched suffix reveal kare jo big-skip rules ko power deta hai.
Mismatch par aur kya hain?
woh pattern index hai jahan pehla (rightmost-first) mismatch hota hai; uske neeche wala bad text character hai.
Ek string ka border kya hota hai?
Ek proper substring jo prefix aur suffix dono ho.
kya hota hai jab mein appear nahi karta?
.
Shift ko se clamp kyun karte hain?
Forward progress guarantee karne ke liye — zero ya negative shift forever loop karega.
Boyer-Moore do rules ke shifts ka kyun leta hai?
Har rule ek independently safe jump hai; do safe jumps mein se bada bhi safe aur faster hota hai.
"Sublinear" yahan kya matlab hai aur yeh kab hota hai?
se kam text characters padhna; bade alphabets par hota hai jahan bad character aksar mein nahi hota, isliye size ke jumps common hain.

🇮🇳 Prefer Hinglish? See 3.8.05 Boyer-Moore — bad character, good suffix heuristics (Hinglish).