Given 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.
If a string is written from left to right and right-to-left, such a string is called a palindromic string, such as ABA, or ABBA. This is the case, given the input of a string, requires the output of a substring, so that the substring is the longest padromic string.
1 Public classSolution {2 Publicstring Longestpalindrome (string s) {3String longets = "";4 if(S.length () <= 1) {5 returns;6 }7 Boolean[] IsP =New Boolean[S.length ()][s.length ()];8 for(inti = 0; I < s.length (); i++) {9Isp[i][i] =true;Ten } One for(inti = 0; I < S.length ()-1; i++) { A if(S.charat (i) = = S.charat (i + 1)) { -Isp[i][i + 1] =true; -Longets = s.substring (i, i + 2); the } - } - - for(intL = 3; L <= s.length (); l++) { + for(inti = 0; I <= s.length ()-l; i++) { - intj = i + l-1; + if(S.charat (i) = =S.charat (j)) { AISP[I][J] = isp[i + 1][j-1]; at if(Isp[i][j] && l >longets.length ()) { -Longets = S.substring (i, j + 1); - } - } - } - } in returnlongets; - to } + - the}
Leetcode Longest palindromic Substring