Topic Chinese : Find the longest palindrome string
Topic difficulty : Medium
topic content :
Given A string s, find the longest palindromic substring in S. Assume that the maximum length of S is 1000.
Translation:
Given a string s, find the longest palindrome substring in S. You can assume that the maximum length of S is 1000.
What is a palindrome string?
Is the string, satisfies can read the inverse reads all the same sub-string, is the palindrome substring. As shown below
Input: "Babad""bab""ABA" is also a valid answer.
My idea : since it's a special substring, then, you have to get each substring first. Then use a method to determine whether it is a palindrome, yes, the length of the answer to compare, longer then update the answer.
MyCode:
1 Publicstring Longestpalindrome (string s) {2 intn =s.length ();3String ans = "";4Set<string> set =NewHashset<string>();5 for(inti = 0; I < n; i++) {6 for(intj = i + 1; J <= N; J + +) {7 Set.add (S.substring (i, J));8 }9 }Ten for(String m:set) { One if(Ispalindromic (m)) { Aans = m.length () >ans.length ()?M:ans; - } - } the returnans; - } - - Public Booleanispalindromic (String m) { +String reverse =NewStringBuffer (M). Reverse (). toString (); - returnM.equals (reverse)?true:false; +}
the complexity of my algorithm : O (N2)
The result is not surprising: timeLimit exceeded
problems that occur during the programming process :
1, because recently in the game with Python, so always forget to define the variable type ...
2, at the beginning of the Palindrome sub-string understanding errors, resulting in programming errors, so ah, or first see the problem, understand the meaning of the topic;
3, string comparison between a hand shake directly with the "= =", hey Equals () Ah, I always sorry you.
Then silly to look at the answer to .....
Answer :
1 classSolution {2String ans = "";3 Publicstring Longestpalindrome (string s) {4 for(inti = 0;i < S.length (); i++) {5 extendverify (s,i,i);6Extendverify (s,i,i+1);7 }8 returnans;9 }Ten Public voidExtendverify (String S,intJintk) { One while(J >= 0 && K < s.length () && S.charat (j) = =S.charat (k)) { Aj--; -k++; - } theString subString = s.substring (j+1, k); -ans = substring.length () >ans.length ()?Substring:ans; - } -}
algorithmic complexity of the answer : O (N) ~ O (N2)
The answer : In two parts, the main method iterates through each character, and then calls the sub-method to expand on both sides by this character, and compares the extended characters of the left and right with two "pointers" .
Two sub-methods are called to verify the left and right singular and even-numbered palindrome with this character as the center .
This code can also be optimized:
1, the main method to judge S, if the length is less than 2, you can directly return s;
2, the main method of the loop end condition can be set to S.length-1, because the last one cannot be extended (but 0 can not be saved, because it is necessary to determine the number of even expansion from the beginning of the (0,1);
3, the last update of the sub-method in fact, only need to determine the length of j+1 to K-1 and a maxlen comparison, and then the starting position J + 1, and the length of MaxLen = k-j-1 To save, you can complete the update.
Leetcode [5] (Java): Longest palindromic Substring Tags: String, dynamic planning