Description
Given a string s, return the longest palindromic substring in s.
Example
Example :
Input: s = “babad”
Output: “bab”
Explanation:
“aba” can also be a valid answer.
Constraints
1 <= s.length <= 1000sconsist of only digits and English letters.
Approach
The solution will be given in Java. The algorithm will be discussed step by step.
Java implementation
public String longestPulindromicSubstring(String s){
if (s == null || s.isEmpty())
return "";
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++){
int len1 = expandAroundCenter(s, i, i);
int len2 = expandAroundCenter(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start){
start = i -(len -1 ) / 2;
end = i + len/2;
}
}
return s.substring(start, end + 1);
}
private int expandAroundCenter(String s, int left, int right){
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)){
L--;
R++;
}
return R - L - 1;
}
Discussion
- The
longestPalindromefunction takes a stringsas input. - It first checks if the string is
nullor empty. If it is, it immediately returns an empty string, as there can be no palindrome in an empty or null string. - The two variables,
startandend, are initialized to keep track of the indices of the longest palindrome substring found so far, and initially, they are set to 0. - The algorithm enters a loop that iterates through each character in the string
s. The loop uses an index variableithat starts from 0 and goes up tos.length() - 1. - For each character at index
i, the algorithm calls theexpandAroundCenterfunction twice to find palindromes centered at the current character or between the current character and the next character. - The
expandAroundCenterfunction takes three parameters: the strings, the left index (i), and the right index (iori + 1depending on the case). It uses these indices to expand outward from the center, checking if the characters at the left and right indices are equal. The function continues expanding as long as the characters match and also as long as the indices are within the bounds of the string. - The
expandAroundCenterfunction returns the length of the palindrome found by subtracting the right index from the left index and subtracting 1. An example is , if the palindrome length is 5, the difference between the indices would be 4. - Back in the
longestPalindromefunction, the algorithm calculates the maximum length of the palindromes found by taking the maximum between the lengths obtained from the two calls toexpandAroundCenter. - If the length of the current palindrome is greater than the length of the previously recorded longest palindrome (
end-start), the algorithm updates thestartandendindices to reflect the new longest palindrome. - After the loop finishes iterating through all characters in the string, the function returns the substring of
sthat corresponds to the longest palindrome, using thestartandendindices.
Check out my other article or Algorithm – Reverse words in a string here
Our Facebook page is here. You can also find our Youtube channel here.
