Given a string s, return the longestpalindromicsubstring in s.
Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Pseudocode
- use a function that is able to extend outwards to compare chars
- in a for loop, use function to cover both odd and even cases
- e.g. for loop travels to middle of string and finds two palindrome char
- function extends outwards to assess how long this palindrome is
- store start and end position of longest palindrome
- return substring with starting and ending index
Solution
// from solution - does the same thingvarlongestPalindrome=function (s) {constlen=s.length;let start =0;let end =0;functionextendPalindrome(str, e) {while (str >=0&& e < len && s[str] === s[e]) {if (e - str > end - start) [start, end] = [str, e]; str--; e++; } }for (let i =0; i < len; i++) {extendPalindrome(i, i); // for odd lengthextendPalindrome(i, i +1); // for even length }returns.slice(start, end +1);};