5. Longest Palindromic Substring

munisekharudavalapati

Munisekhar Udavalapati

Posted on November 1, 2024

5. Longest Palindromic Substring

Given a string s, return the longest
palindromic

substring
in s.

Example 1:
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.

Example 2:
Input: s = "cbbd"
Output: "bb"

Enter fullscreen mode Exit fullscreen mode

Constraints:

1 <= s.length <= 1000
s consist of only digits and English letters.

var longestPalindrome = function(s) {
     let res = "";
    let max = 0;

    for(let i = 0; i < s.length; i++){
        for(let j = 0; j <= 1; j++){
            let l = i;
            let r = i + j;

            while(l >= 0 && r < s.length && s[l] === s[r]){
                let len = r - l + 1;
                if(len > max){
                    res = s.substring(l, r + 1);
                    max = len;
                }
                l--;
                r++;
            }
        }
    }
    return res;
};
Enter fullscreen mode Exit fullscreen mode
๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
munisekharudavalapati
Munisekhar Udavalapati

Posted on November 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

5. Longest Palindromic Substring
javascript 5. Longest Palindromic Substring

November 1, 2024

4. Median of Two Sorted Arrays
javascript 4. Median of Two Sorted Arrays

October 31, 2024

Big O notation
algorithms Big O notation

October 26, 2024

ยฉ TheLazy.dev

About