Manacher's Algorithm
Finds longest palindromic substring in O(n) using symmetry.
Visualization
Interactive visualization for Manacher's Algorithm
Manacher's Algorithm (Longest Palindrome)
• Time: O(n) linear time!
• Finds longest palindromic substring
• Uses center expansion with clever reuse
Interactive visualization with step-by-step execution
Implementation
1function manacher(s: string): string {
2 const t = '#' + s.split('').join('#') + '#';
3 const P = new Array(t.length).fill(0);
4 let C = 0, R = 0, maxLen = 0, centerIdx = 0;
5
6 for (let i = 0; i < t.length; i++) {
7 if (i < R) P[i] = Math.min(R - i, P[2*C - i]);
8 while (t[i + P[i] + 1] === t[i - P[i] - 1]) P[i]++;
9 if (i + P[i] > R) { C = i; R = i + P[i]; }
10 if (P[i] > maxLen) { maxLen = P[i]; centerIdx = i; }
11 }
12
13 return s.substring((centerIdx - maxLen)/2, (centerIdx - maxLen)/2 + maxLen);
14}Deep Dive
Theoretical Foundation
Uses palindrome symmetry to avoid redundant checks. Maintains center and right boundary.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
DNA sequence analysis (finding palindromic sequences)
Text processing and pattern recognition
Bioinformatics (restriction enzyme sites)
Compiler design (lexical analysis)
Data compression algorithms
Cryptographic applications
Natural language processing
Use Cases
Related Algorithms
Knuth-Morris-Pratt (KMP) Algorithm
An efficient string pattern matching algorithm that searches for occurrences of a 'word' within a 'text' by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to determine where the next match could begin. Developed by Donald Knuth, Vaughan Pratt, and James H. Morris in 1977, it's one of the most important string algorithms with O(n+m) time complexity.
Rabin-Karp Algorithm
A string-searching algorithm that uses hashing to find pattern(s) in a text. Developed by Michael O. Rabin and Richard M. Karp in 1987, it's particularly useful for multiple pattern search and plagiarism detection. Uses rolling hash for efficiency.
Boyer-Moore Algorithm
One of the most efficient string searching algorithms in practice, using two heuristics: bad character rule and good suffix rule. Developed by Robert S. Boyer and J Strother Moore in 1977, it's the standard benchmark for practical string search, often outperforming other algorithms by skipping sections of text.
Aho-Corasick Algorithm
A string-searching algorithm for locating elements of a finite set of strings (dictionary) within an input text. Invented by Alfred V. Aho and Margaret J. Corasick in 1975, it's a kind of dictionary-matching algorithm that simultaneously searches for all patterns in linear time, making it extremely efficient for multiple pattern matching.