3.8.7 · D5 · HinglishString Algorithms

Question bankSuffix array — construction O(n log n), LCP array

1,551 words7 min read↑ Read in English

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


True or false — justify

Every string of length has a suffix array that is a permutation of .
True — exactly suffixes hote hain, sab alag-alag indices pe start hote hain, aur har ek sorted order mein ek baar aata hai, isliye hamesha par ek bijection hota hai.
Two different suffixes of the same string can be exactly equal, so the sorted order has ties.
False — suffixes ki lengths alag hoti hain (woh alag indices pe start hote hain), isliye koi bhi do exactly identical nahi ho sakte; chhota wala zyada se zyada lambe ka strict prefix ho sakta hai, aur prefix hamesha strictly pehle sort hota hai.
measures the shared prefix of suffixes starting at index and index .
False — yeh sorted neighbours aur ka LCP hai; indices string mein kahin bhi ho sakte hain, aur pe nahi necessarily.
If for some , then and start with different first characters.
True — LCP ka matlab hai koi bhi leading character share nahi hota, isliye unke pehle letters differ karte hain (yahi mein "letter blocks" ke beech boundary hai).
The rank array and the suffix array contain the same information.
True — rank, ki inverse permutation hai (), isliye dono ek-doosre ko mein reconstruct kar sakte hain; yeh usi ordering ke do views hain.
Prefix-doubling needs exactly rounds regardless of the string.
True (ceiling tak) — double hota hai jab tak na ho, isliye hamesha rounds lagte hain; string ka content ranks change karta hai lekin rounds ki ginti kabhi nahi.
Kasai's algorithm processes suffixes in sorted () order.
False — yeh indices ko string order mein walk karta hai; wahi ordering hai jo " at most 1 se girta hai" reuse ko valid banati hai.
The number of distinct substrings of equals .
True — har suffix ke har prefix ko count karta hai (multiplicity ke saath sab substrings), aur har exactly woh substrings subtract karta hai jo sorted neighbours ke beech duplicate hain.
A suffix array plus an LCP array can answer "LCP of any two suffixes" (not just neighbours) in .
True — aur ka LCP ka minimum hota hai, jise Sparse Table / RMQ preprocessing ke baad mein answer karta hai.

Spot the error

"To compare two suffixes on their first chars I need to compare characters directly."
Wrong — doubling ka poora point yahi hai ki tum do known -rank halves ko jodoge: key hai pair , isliye har comparison hai, nahi.
"For I'll use since there's nothing there."
Wrong — ek valid rank hai, isliye end-of-string pe pahuncha suffix ek real suffix ke saath tie/outrank kar sakta hai; end-of-string ko lexicographically smallest encode karne ke liye (har real rank se chhota) use karo.
"Each round of doubling with a comparison sort keeps the total at ."
Wrong — comparison sort per round hai, rounds ke upar hota hai; sirf Radix Sort (ya Counting Sort) per round true deta hai.
"In Kasai I reset at the start of every to be safe."
Wrong — yeh amortization throw away kar deta hai aur ise bana deta hai; tumhe ko iterations ke across carry karna hai, har step se pehle sirf karo.
"After radix-sorting pairs I can reuse the same array in place while assigning ."
Wrong — naye ranks assign karte waqt tum ties detect karne ke liye purane ranks chahiye; sab comparisons khatam hone se pehle overwrite karo toh equal pairs ko alag ranks milenge, order corrupt ho jayega. Ek alag temp array use karo.
" is the LCP of the first suffix with the last one."
Wrong — ka koi predecessor nahi hai isliye convention se yeh define hota hai; yeh wrap-around comparison nahi hai.
"Since suffixes overlap, binary-searching a pattern might match a non-contiguous set of positions."
Wrong — kisi given prefix wale sab suffixes sorted order mein consecutive hote hain, isliye pattern ki occurrences hamesha ka ek contiguous block banati hain (isliye hi Binary Search on Suffix Array kaam karta hai).

Why questions

Why does sorting by the pair correctly give the order by first characters?
Kyunki suffix ke pehle chars split hote hain (suffix ke pehle ) aur phir (suffix ke pehle ) ke roop mein; us concatenation par lexicographic order exactly unke ranks ke pair par lexicographic order hai.
Why is end-of-string treated as smaller than every real character?
Kyunki ek chhota suffix jo lambe ka prefix hai woh pehle sort hona chahiye (jaise dictionary mein "an" "ana" se pehle), aur missing character ko sabse chhoti possible value model karna exactly wahi rule enforce karta hai.
Why does the LCP with the sorted predecessor drop by at most 1 when moving from index to ?
Agar apne predecessor ke saath chars share karta hai, toh aur share karte hain aur phir bhi se pehle sort hota hai, isliye ka true predecessor kam se kam share karta hai.
Why is a suffix array often preferred over a Suffix Tree despite the tree being "more powerful"?
Array ek flat integer array hai — bahut chhota constant-factor memory aur implement karna kaafi simple — tree ki zyaadatar query power (substring search, repeats, distinct counts) deta hai bahut kam takleef mein.
Why do we need the rank array inside Kasai rather than just ?
Hum suffixes ko starting index se visit karte hain aur instantly jaanna chahte hain ki sorted order mein kahaan hai taaki uska predecessor mil sake; sirf inverse permutation rank yeh mein deta hai.
Why does distinct-substring counting subtract exactly the LCP values?
Sorted neighbours apne pehle prefixes share karte hain, isliye baad wale suffix ke woh substrings pehle waale ne already count kar liye the; unhe subtract karna double-counting exactly ek baar remove karta hai.

Edge cases

What is the suffix array of a single-character string like "z"?
— exactly ek suffix hai, aur LCP array sirf hai (compare karne ke liye koi adjacent pair nahi hai).
What does the LCP array look like for a string of all identical characters, e.g. "aaaa"?
Suffixes length ke hisaab se sort hote hain () aur har neighbour extra leading char ko chhodkar sab kuch share karta hai, isliye — maximum staircase.
For a string of all distinct characters like "abcd", what is every LCP value?
Sab hote hain ke liye — sort hone ke baad koi bhi do suffixes pehla character share nahi karte, isliye neighbours ka koi common prefix nahi.
How many doubling rounds run when ?
Initial single-character ranking ke baad zero effective rounds, kyunki already hai; initial rank hi final order hai.
What happens in Kasai when (suffix is the smallest)?
Iska koi sorted predecessor nahi hai, isliye hum set karte hain aur kuch extra record nahi karte; agla simply is floor se fresh start karta hai.
Does appending a unique sentinel character (like $, smaller than all letters) change the suffix array of the rest?
Yeh ek sabse chhota suffix add karta hai jo pehle sort hota hai aur guarantee deta hai ki koi suffix doosre ka prefix nahi hai, jo proofs aur kuch constructions simplify karta hai bina original suffixes ke relative order disturb kiye.

Recall Jaane se pehle ek-line self-test

Kya tum bina dekhe bata sakte ho: (1) kin do suffixes ko compare karta hai, (2) off-end rank kyun hota hai, (3) Kasai mein carry kyun karna chahiye, aur (4) doubling rounds kyun deta hai? Agar koi bhi fuzzy lag raha hai, upar woh trap dobara padho.