Skip to content

Commit 01ed356

Browse files
committed
to see md effect
1 parent 2179d92 commit 01ed356

File tree

10 files changed

+7
-54
lines changed

10 files changed

+7
-54
lines changed

questions/longest-palindromic-substring-part-ii.md renamed to question/longest-palindromic-substring-part-ii.md

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
给定一个字符串S,找出S中最长的回文子字符串。
55

6-
Note:
7-
This is Part II of the article: Longest Palindromic Substring. Here, we describe an algorithm (Manacher’s algorithm) which finds the longest palindromic substring in linear time. Please read Part I for more background information.
86
注意:
9-
这是最长回文子字符串 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 的第二部。
7+
这是最长回文子字符串 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 的第二部。这里,我们要描述一个算法(Manacher算法),可以在线性时间内找出最长回文子字符串。请阅读第一部 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 了解更多的内幕(傲骄了)。
8+
9+
Manacher
10+
![ScreenShot](https://raw.github.com/xiangzhai/goaxel/master/image/ManG490.jpg)
1011

11-
In my previous post we discussed a total of four different methods, among them there’s a pretty simple algorithm with O(N2) run time and constant space complexity. Here, we discuss an algorithm that runs in O(N) time and O(N) space, also known as Manacher’s algorithm.
1212

1313
Hint:
1414
Think how you would improve over the simpler O(N2) approach. Consider the worst case scenarios. The worst case scenarios are the inputs with multiple palindromes overlapping each other. For example, the inputs: “aaaaaaaaa” and “cabcbabcbabcba”. In fact, we could take advantage of the palindrome’s symmetric property and avoid some of the unnecessary computations.
@@ -62,56 +62,7 @@ The final part is to determine when should we move the position of C together wi
6262
If the palindrome centered at i does expand past R, we update C to i, (the center of this new palindrome), and extend R to the new palindrome’s right edge.
6363
In each step, there are two possibilities. If P[ i ] ≤ R – i, we set P[ i ] to P[ i' ] which takes exactly one step. Otherwise we attempt to change the palindrome’s center to i by expanding it starting at the right edge, R. Extending R (the inner while loop) takes at most a total of N steps, and positioning and testing each centers take a total of N steps too. Therefore, this algorithm guarantees to finish in at most 2*N steps, giving a linear time solution.
6464

65-
66-
1
67-
2
68-
3
69-
4
70-
5
71-
6
72-
7
73-
8
74-
9
75-
10
76-
11
77-
12
78-
13
79-
14
80-
15
81-
16
82-
17
83-
18
84-
19
85-
20
86-
21
87-
22
88-
23
89-
24
90-
25
91-
26
92-
27
93-
28
94-
29
95-
30
96-
31
97-
32
98-
33
99-
34
100-
35
101-
36
102-
37
103-
38
104-
39
105-
40
106-
41
107-
42
108-
43
109-
44
110-
45
111-
46
112-
47
113-
48
114-
49
65+
```
11566
// Transform S into T.
11667
// For example, S = "abba", T = "^#a#b#b#a#$".
11768
// ^ and $ signs are sentinels appended to each end to avoid bounds checking
@@ -161,6 +112,8 @@ string longestPalindrome(string s) {
161112
162113
return s.substr((centerIndex - 1 - maxLen)/2, maxLen);
163114
}
115+
```
116+
164117
Note:
165118
This algorithm is definitely non-trivial and you won’t be expected to come up with such algorithm during an interview setting. However, I do hope that you enjoy reading this article and hopefully it helps you in understanding this interesting algorithm. You deserve a pat if you have gone this far! :)
166119

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)