Given A stringS, find the longest palindromic substring inS. Assume that maximum length ofSis, and there exists one unique longest palindromic substring.
Given a string s, find the longest back text string in it.
1. Enumerate all substrings and infer if it is a palindrome that records the maximum length at the same time. Timeout.
Find all substrings, and infer whether it is a palindrome string, each record length public static string longestPalindrome1 (string s) {string ret = ""; int maxlen = 0;for (int i=0;i&l T;s.length (); i++) {for (int j=i+1;j<s.length (); j + +) {String temp = s.substring (i,j+1); int len = J-i;if (Ispalindrome ( Temp)) {if (len > maxlen) {ret = Temp;maxlen = Len;}}}} return ret;} public static Boolean ispalindrome (String s) {for (int i=0;i<s.length (); i++) {if (S.charat (i)! = S.charat (s.length ()-I -1)) return false;} return true;}
2. From a center to the two sides of the expansion of the search, find the maximum length can be extended.
public static String Longestpalindrome (string s) {if (S.length () <= 1) return s; string longest = s.substring (0, 1); for (int i = 0; i < s.length (); i++) {String tmp = Helper (s, I, I); if (Tmp.length () > Longest.length ()) {longest = tmp;} TMP = Helper (s, I, i + 1), if (Tmp.length () > Longest.length ()) {longest = tmp;}} return longest;} public static string helper (string s, int begin, int end) {while (Begin >= 0 && end <= s.length ()-1 && Amp S.charat (BEGIN) = = S.charat (end)) {begin--;end++;} return s.substring (begin + 1, end);}
Leetcode--longest palindromic Substring