If t has length m=2n+1, we compute an array ==P[i]== = radius of the longest palindrome centered at t[i] (radius = number of characters on each side, in t).
We sweep i left to right and maintain the rightmost-reaching palindrome found so far, described by its center C and right boundary R (so it covers [C−(R−C),R], i.e. half-length R−C).
def manacher(s): t = '#' + '#'.join(s) + '#' # length 2n+1 m = len(t) P = [0]*m C = R = 0 for i in range(m): if i < R: P[i] = min(P[2*C - i], R - i) # mirror, capped at wall # expand while i - P[i] - 1 >= 0 and i + P[i] + 1 < m and \ t[i - P[i] - 1] == t[i + P[i] + 1]: P[i] += 1 # update rightmost if i + P[i] > R: C, R = i, i + P[i] return P # P[i] = palindrome length in ORIGINAL s centered at t[i]# longest palindromic substring:def longest_pal(s): P = manacher(s) k = max(range(len(P)), key=lambda i: P[i]) start = (k - P[k]) // 2 # map t-index back to s-index return s[start:start + P[k]]
Imagine a row of mirror tiles where the middle is a magic mirror. If you already know the left side perfectly reflects the right side, then to know what's at some spot, you can just peek at its mirror buddy instead of measuring again. You only do real measuring at the very edge where the mirror's reflection runs out. Because that edge only ever moves forward, you never re-measure the same ground twice — so the whole job is fast. The # symbols are just spacers we sprinkle in so that "even-length" mirrors and "odd-length" mirrors become the same shape and we only have to deal with one kind.
Manacher ka core idea bahut simple hai: palindrome symmetric hota hai, yaani left side right side ka mirror hota hai. Toh agar tumne pehle ek bada palindrome already find kar liya hai, toh uske andar ki har cheez already mirror ho chuki hai. Naya center aata hai toh tum scratch se ginna shuru mat karo — apne mirror position ka answer copy kar lo, aur sirf utna hi expand karo jitna mirror guarantee nahi de raha. Isi reuse ki wajah se naive O(n2) ban jaata hai fast O(n).
Pehle ek trick: original string mein # daal do har character ke beech (abba → #a#b#b#a#). Isse saare palindrome odd-length ban jaate hain, toh odd aur even ka alag-alag case handle karne ki tension khatam. Phir array P[i] banao = us center ka palindrome radius. Mazedaar baat: ye radius bilkul original string ke palindrome ki length ke barabar hota hai.
Main formula: agar current i already-known bade palindrome ke andar hai (center C, right wall R), toh mirror nikaalo i′=2C−i aur P[i] = min(P[i'], R-i) set karo. min isliye, kyunki wall R ke baad humein kuch pata nahi — symmetry ki guarantee wahin khatam ho jaati hai. Uske baad character-by-character expand karo, aur agar tum R se aage nikal gaye toh (C,R) update kar do.
Kyun fast? Mirror-copy O(1) hai, aur expand loop sirf tab kaam karta hai jab R aage badhta hai — aur R kabhi peeche nahi jaata, sirf 0 se 2n+1 tak aage. Toh total expansion O(n). Exam/interview mein ye dhyaan rakho: min mein wall cap mat bhoolna, aur original index map karte time (k - P[k]) // 2 use karna — ye do hi sabse common galtiyaan hain.