Longest palindromic Substring--Hard level
Question Solutiongiven A string s, find the longest palindromic substring in S. The maximum length of S is assume, and there exists one unique longest palindromic substring.
Classic DP topic.
Home Page June gives 3 solutions:
1. Brute Force
GitHub Code Links
This very straight Forward, that is, start node I traversal from 0-len-1 once, end node to i-len-1 traversal once. Each string is
Judge whether it is a palindrome, judge is not a palindrome can also use the return to do. The total complexity is
Time:o (n^3), Space:o (1)
2. DP
DP because it's a two-dimensional dynamic programming
Time:o (n^2), Space:o (n^2)
1 Publicstring Longestpalindrome (string s) {2 if(s = =NULL) {3 return NULL;4 }5 6String ret =NULL;7 8 intLen =s.length ();9 intMax = 0;Ten One Boolean[] D =New Boolean[Len][len]; A - for(intj = 0; J < Len; J + +) { - for(inti = 0; I <= J; i++) { theD[I][J] = S.charat (i) = = S.charat (j) && (j-i <= 2 | | D[i + 1][j-1]); - if(D[i][j]) { - if(J-i + 1 >max) { -max = j-i + 1; +ret = S.substring (i, j + 1); - } + } A } at } - - returnret; -}
View Code
Please poke the code of the homepage June OH
Explanation:
The status expression: D[i][j] indicates that the string between these 2 indexes is not a palindrome i,j.
Recursive formula: D[i][j] = if (char i = = char j) && (D[i + 1][j-1] | | J-i <= 2) This is a good understanding, with recursion is a meaning, but the advantage of the rules of motion is that we can reuse these results.
Initialization
D[i][i] = true; in fact, it does not have to be specifically initialized, it can be nested inside a recursive formula. So the home page code will look simple.
Note: with Max record palindrome length, palindrome found, update Max, and the starting address of the result, end address.
Now that's the point, how do we design this move to reuse it?
From here can be seen D[i + 1][j-1], we push i,j when used to I+1, j-1, in fact, the meaning is in the calculation i,j, about the same j-1 all I must be calculated.
Draw as follows:
1.00
2.00 01
11
3.00 01 02
11 12
22
3.00 01 02 03
11 12 13
22 23
33
See the recursive relationship above? As long as we have a list of calculations, we are able to successfully use this dynamic formula. This is also the key design for dynamic planning.
If you don't know how to design, just like the home page group, draw a graph to see what values are needed when we calculate a value. As shown, what we need is i+1, j-1, which is actually the lower-left value, so that we can successfully plan for a single column of calculations.
Note: A one-line calculation will fail!
So the design of our loop is this:
for (int j = 0; J < Len; j + +)
{for (int i = 0; I <= J; i++) {
For details, see the code, and feel it slowly. This is the essence of the rules of motion.
3. The Central Expansion Act.
This method is actually very intuitive, is to scan from the beginning to the tail, each character with it as the center of the 2-side extension, expand to not expand until (there are different characters), return the palindrome centered on each character, and then constantly update the maximum palindrome and return.
The algorithm is simple, and the complexity is O (n^2), the space complexity is O (1)
Recommended interview Use this method. It is said that some companies such as ebay will reject the solution of the rules. ORZ. Actually homepage June compare Heart Water second solution Ah, how beautiful, the third solution, although less consumption, but there is no universality.
1 Public classSolution {2 Publicstring Longestpalindrome (string s) {3 if(s = =NULL) {4 return NULL;5 }6 7String ret =NULL;8 9 intLen =s.length ();Ten intMax = 0; One for(inti = 0; i < Len; i++) { AString S1 =Getlongest (S, I, I); -String s2 = getlongest (s, I, i + 1); - the if(S1.length () >max) { -Max =Math.max (Max, s1.length ()); -RET =S1; - } + - if(S2.length () >max) { +Max =Math.max (Max, s2.length ()); ARET =S2; at } - } - - returnret; - } - in PublicString Getlongest (String s,intLeftintRight ) { - intLen =s.length (); to while(left >= 0 && right <Len) { + //When I am in the Center. - if(S.charat (left)! =S.charat (right)) { the Break; * } $ Panax Notoginsengleft--; -right++; the } + A returnS.substring (left + 1), right); the } +}
View Code
Center Expansion Method Code
Other methods of comparing cattle: http://blog.csdn.net/hopeztm/article/details/7932245
ref:http://blog.csdn.net/fightforyourdream/article/details/21309759
http://blog.163.com/zhaohai_1988/blog/static/2095100852012716105847112/
http://blog.csdn.net/fightforyourdream/article/details/15025663
Leetcode: "DP" longest palindromic Substring Problem solving report