Skip to content

Commit 8f8ad15

Browse files
ulwlusiriak
andauthored
Add manacher algorithm (rust-lang#183)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent b84e503 commit 8f8ad15

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ These are for demonstration purposes only.
5555
## [Strings](./src/string)
5656

5757
- [x] [Knuth Morris Pratt](./src/string/knuth_morris_pratt.rs)
58+
- [x] [Manacher](./src/string/manacher.rs)
5859
- [x] [Rabin Carp](./src/string/rabin_karp.rs)
5960

6061
## [General](./src/general)

src/string/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ __Properties__
1010

1111
[kmp-wiki]: https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm
1212

13+
### [Manacher](./manacher.rs)
14+
From [Wikipedia][manacher-wiki]: find a longest palindrome in a string in linear time.
15+
16+
__Properties__
17+
* Worst-case time complexity is O(n)
18+
* Worst-case space complexity is O(n)
19+
20+
[manacher-wiki]: https://en.wikipedia.org/wiki/Longest_palindromic_substring#Manacher's_algorithm
1321
### [Rabin Karp](./rabin_karp.rs)
1422
From [Wikipedia][rabin-karp-wiki]: a string-searching algorithm created by Richard M. Karp and Michael O. Rabin that uses hashing
1523
to find an exact match of a pattern string in a text.

src/string/manacher.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
pub fn manacher(s: String) -> String {
2+
let l = s.len();
3+
if l <= 1 {
4+
return s;
5+
}
6+
7+
// MEMO: We need to detect odd palindrome as well,
8+
// therefore, inserting dummy string so that
9+
// we can find a pair with dummy center character.
10+
let mut chars: Vec<char> = Vec::with_capacity(s.len() * 2 + 1);
11+
for c in s.chars() {
12+
chars.push('#');
13+
chars.push(c);
14+
}
15+
chars.push('#');
16+
17+
// List: storing the length of palindrome at each index of string
18+
let mut length_of_palindrome = vec![1usize; chars.len()];
19+
// Integer: Current checking palindrome's center index
20+
let mut current_center: usize = 0;
21+
// Integer: Right edge index existing the radius away from current center
22+
let mut right_from_current_center: usize = 0;
23+
24+
for i in 0..chars.len() {
25+
// 1: Check if we are looking at right side of palindrome.
26+
if right_from_current_center > i && i > current_center {
27+
// 1-1: If so copy from the left side of palindrome.
28+
// If the value + index exceeds the right edge index, we should cut and check palindrome later #3.
29+
length_of_palindrome[i] = std::cmp::min(
30+
right_from_current_center - i,
31+
length_of_palindrome[2 * current_center - i],
32+
);
33+
// 1-2: Move the checking palindrome to new index if it exceeds the right edge.
34+
if length_of_palindrome[i] + i >= right_from_current_center {
35+
current_center = i;
36+
right_from_current_center = length_of_palindrome[i] + i;
37+
// 1-3: If radius exceeds the end of list, it means checking is over.
38+
// You will never get the larger value because the string will get only shorter.
39+
if right_from_current_center >= chars.len() - 1 {
40+
break;
41+
}
42+
} else {
43+
// 1-4: If the checking index doesn't exceeds the right edge,
44+
// it means the length is just as same as the left side.
45+
// You don't need to check anymore.
46+
continue;
47+
}
48+
}
49+
50+
// Integer: Current radius from checking index
51+
// If it's copied from left side and more than 1,
52+
// it means it's ensured so you don't need to check inside radius.
53+
let mut radius: usize = (length_of_palindrome[i] - 1) / 2;
54+
radius += 1;
55+
// 2: Checking palindrome.
56+
// Need to care about overflow usize.
57+
while i >= radius && i + radius <= chars.len() - 1 && chars[i - radius] == chars[i + radius]
58+
{
59+
length_of_palindrome[i] += 2;
60+
radius += 1;
61+
}
62+
}
63+
64+
// 3: Find the maximum length and generate answer.
65+
let center_of_max = length_of_palindrome
66+
.iter()
67+
.enumerate()
68+
.max_by_key(|(_, &value)| value)
69+
.map(|(idx, _)| idx)
70+
.unwrap();
71+
let radius_of_max = (length_of_palindrome[center_of_max] - 1) / 2;
72+
let answer = &chars[(center_of_max - radius_of_max)..(center_of_max + radius_of_max + 1)]
73+
.iter()
74+
.collect::<String>();
75+
answer.replace("#", "")
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::manacher;
81+
82+
#[test]
83+
fn get_longest_palindrome_by_manacher() {
84+
assert_eq!(manacher("babad".to_string()), "aba".to_string());
85+
assert_eq!(manacher("cbbd".to_string()), "bb".to_string());
86+
assert_eq!(manacher("a".to_string()), "a".to_string());
87+
88+
let ac_ans = manacher("ac".to_string());
89+
assert!(ac_ans == "a".to_string() || ac_ans == "c".to_string());
90+
}
91+
}

src/string/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
mod knuth_morris_pratt;
2+
mod manacher;
23
mod rabin_karp;
34

45
pub use self::knuth_morris_pratt::knuth_morris_pratt;
6+
pub use self::manacher::manacher;
57
pub use self::rabin_karp::rabin_karp;

0 commit comments

Comments
 (0)