For example you have string, where you need to find longest palindromic(symmetrical) substring with the best Big-O, how would you do that?
Example 1
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Here is my solution, but it is too slow for this type of problem.
public String longestPalindrome(String s) {
if (s == null || s.length() == 0)
return s;
String longest = s.substring(0, 1);
for (int i = 0; i < s.length(); i++) {
if(s.length()-i <longest.length()/2)
break;
String oddPal = findLengthofPalindrome(s, i, i);
if (longest.length() < oddPal.length()) {
longest = oddPal;
}
String evenPal = findLengthofPalindrome(s, i, i + 1);
if (longest.length() < evenPal.length()) {
longest = evenPal;
}
}
return longest;
}
private String findLengthofPalindrome(String s, int left, int right) {
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
}
return s.substring(left + 1, right);
}