Title Link: https://oj.leetcode.com/problems/longest-palindromic-substring/
This problem can usually be written out of 2 kinds of practice.
1. Keep an index from the go back sweep, each index cycle, keep left and right index to sweep on both sides, but there are 2 cases, ABA and ABBA. Left and right scan to sweep 2 times
2. DP procedure, 2-D matrix
* Can be properly reduced, such as the current index is completely impossible to provide than the longest string has been obtained longer than possible, then move to the next index
Class Solution {Public:string Longestpalindrome (string s) {if (s.size () = = 0) return ""; int idx = 0; int maxlen = 0; string res; while (IDX < s.size ()) {//break if even the longest palindrome string we could get//is still Shorter than the Maximun one we have an if (min (s.size ()-idx, IDX + 1) * 2 <= maxlen) break; int left; int right; 1st case, Ex:aba left = idx; right = IDX; while (left >= 0 && right < S.size ()) {if (s[left] = = S[right]) {if (max Len < Right-left + 1) {MaxLen = right-left + 1; res = S.substr (left, right-left + 1); }--left; ++right; } else {break; } } 2nd case, Ex:abba left = idx; right = idx + 1; while (left >= 0 && right < S.size ()) {if (s[left] = = S[right]) {if (max Len < Right-left + 1) {MaxLen = right-left + 1; res = S.substr (left, right-left + 1); }--left; ++right; } else {break; }} ++idx; } return res; }};
[Leetcode] 5-longest palindromic Substring