5. Longest Palindromic Substring
Given a string s, return the longest palindromic substring in s.
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Input: s = "cbbd"
Output: "bb"
Solution
- Loop through each character of s
- Check if s[i] or s[i] + s[i + 1] is center of a palindromic
function longestPalindrome(s: string): string {
if (!s || s.length <= 1) { return s }
let longestPalindrome = s.substring(0, 1)
for (let i = 0; i < s.length; i++) {
[expand(s, i, i), expand(s, i, i + 1)].forEach((maybeLongest) => {
if (maybeLongest.length > longestPalindrome.length) {
longestPalindrome = maybeLongest
}
})
}
return longestPalindrome
}
function expand(s: string, begin: number, end: number): string {
while (begin >= 0 && end <= s.length - 1 && s[begin] === s[end]) {
begin--
end++
}
return s.substring(begin + 1, end)
}