3.8.7 · D1 · HinglishString Algorithms

FoundationsSuffix array — construction O(n log n), LCP array

2,102 words10 min read↑ Read in English

3.8.7 · D1 · Coding › String Algorithms › Suffix array — construction O(n log n), LCP array

Yeh page woh har symbol build karta hai jo parent note use karta hai, "string kya hoti hai" se shuru karke — aur koi bhi notation use karne se pehle usse draw aur explain karta hai. Agar neeche koi symbol already obvious lage, skim karo — lekin baad ke har idea ka base pehle waale ideas hain, isliye order matter karta hai.


1. Ek string aur uske characters

Ek row of numbered boxes ki picture socho. Box number 0 pehla letter rakhta hai.

Figure — Suffix array — construction O(n log n), LCP array
  • Topic ko iske zaroorat kyun hai: suffix array hi positions ki list hai, isliye "position" ka concept bilkul solid hona chahiye.

2. Ek suffix — string ki "tail"

Agar toh (poori cheez), , aur (sirf aakhri letter). Exactly suffixes hote hain — ek har position par shuru hota hua.

Figure — Suffix array — construction O(n log n), LCP array
  • Topic ko iske zaroorat kyun hai: suffix array inhi objects ko sort karta hai.

3. Prefix aur "longest common prefix"

Dono strings ko left-aligned line up karo aur across read karo pehle mismatch tak. Match karne wale columns ki ginti LCP length hai.

  • Topic ko iske zaroorat kyun hai: poora LCP array (parent ka Section 3) bas yahi number hai jo neighboring sorted suffixes ke liye compute hota hai.

4. Lexicographic order — "dictionary order"

Single characters ke liye hum ordinary alphabet order reuse karte hain: . "banana" mein sirf letters hain with .

Figure — Suffix array — construction O(n log n), LCP array
  • Topic ko iske zaroorat kyun hai: "sorted lexicographically" hi suffix array ki definition hai.

5. Suffix array aur ka matlab

Isko aise padho: "sorted bookshelf par shelf number par jao; woh tail kis position par start hui thi?"

Figure — Suffix array — construction O(n log n), LCP array
  • Topic ko iske zaroorat kyun hai: construction ka output hi hai. Baad ki har cheez (binary search, LCP) isko read karti hai.

6. Rank array — ka inverse

Agar toh position shelf par hai, position shelf par, aur isi tarah, milta hai (padhna: position shelf par hai).

  • Topic ko iske zaroorat kyun hai: predecessor lookup literally dono arrays ko chain karta hai.

7. Subscript aur doubling ranks

se shuru karo (sirf pehle letter se sort karo), phir , , , har round mein doubling karo jab tak na ho, us point par "first chars" already poore suffix ko cover kar leta hai.

Figure — Suffix array — construction O(n log n), LCP array
  • Topic ko iske zaroorat kyun hai: yahi engine hai. doublings ki ginti hai.

8. Big-O notation aur

  • Topic ko iske zaroorat kyun hai: doubling rounds ki ginti exactly hai, isliye cost label ka matlab hai "har doubling round mein linear kaam × rounds ki ginti."

9. Letter — Kasai mein running LCP length

  • Topic ko iske zaroorat kyun hai: hi wajah hai ki LCP construction ki jagah hai; carry na karo toh sab kuch re-scan karna padta hai.

Prerequisite map

Characters s at i (0-indexed)

Suffix suf of i

Prefix and LCP length

Character order a lt b lt n

Lexicographic order of strings

Suffix array SA of r

Rank array inverse of SA

Doubling ranks r sub k

Construction O n log n

Kasai LCP with carried h

Parent topic


Equipment checklist

Self-test: kya tum reveal karne se pehle har ek ka jawaab de sakte ho?

ka kya matlab hai aur counting kahan se shuru hoti hai?
Position par character, 0 se count karte hue, toh pehla aur aakhri hai.
kya hai?
ki tail position se end tak, ; inki ginti hoti hai.
Kyun har substring ka suffix se relation hai?
Har substring kisi suffix ka prefix (front chunk) hota hai, isliye suffixes padhne se saare substrings cover ho jaate hain.
Do strings ko lexicographically kaise compare karte hain?
Left se character by character compare karo; pehla mismatch order decide karta hai; agar ek match karte karte khatam ho jaaye, toh chhoti wali smaller hoti hai.
ko words mein kya kehte hain?
Dictionary order mein -th smallest suffix ka starting index.
aur mein kya relation hai?
Yeh inverse permutations hain: ; shelf→position map karta hai, position→shelf map karta hai.
kya hai aur doubling kyun?
Suffix ki rank sirf uske pehle chars use karke; doubling se first chars ko pair ke roop mein compare kar sakte hain already-known ranks use karke.
Jab ho toh kyun use hota hai?
Woh suffix jo end se bahar chala gaya woh empty/smallest character ki tarah act karta hai, isliye use kisi bhi real character se pehle sort karna chahiye — se encode kiya jaata hai.
Yahan ka kya matlab hai?
Lagbhag doubling rounds, har ek mein linear () kaam.
Kasai's algorithm mein kya karta hai?
Current overlap length ko positions ke across carry karta hai, har step mein at most 1 se shrink karta hai, linear-time LCP deta hai.