Given a string s, return the longest palindromic substring in s.

&nbsp;
Example 1:

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


Example 2:

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


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 1000
	s consist of only digits and English letters.


class Solution:
    def longestPalindrome(self, s: str) -> str:
        res = ""
        for i in range(len(s)):
            left, right = i - 1, i + 1

            while (right < len(s) and s[right] == s[i]):
                right += 1

            while (0 <= left < right < len(s) and s[left] == s[right]):
                left, right = left - 1, right + 1

            res = s[left+1:right] if right - left-1 > len(res) else res
        return res
class Solution {
    String max = "";
    
    private void checkPalindrome(String s, int l, int r) {
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            if (r - l >= max.length()) {
                max = s.substring(l, r + 1);
            }   

            l--;
            r++;
        }
    }
    
    public String longestPalindrome(String s) {
        for (int i = 0; i < s.length(); i++) {
            checkPalindrome(s, i, i);
            checkPalindrome(s, i, i + 1);   
        }
        
        return max;
    }
}
class Solution {
public:
    string longestPalindrome(string s) {
        if(s.size() <= 1) return s;

        string longest = "";

        for (int i = 0; i < s.size(); i++) {
            string sub1 = expand(s, i, i+1);
            string sub2 = expand(s, i, i);

            string sub3 = sub1.size() > sub2.size() ? sub1 : sub2;

            if(sub3.size() > longest.size()) {
                longest = sub3;
            }
        }
        return longest;
    }

    string expand(string s, int i, int j) {
        while(j < s.size() && i >= 0 && s[i] == s[j]) {
            i--;
            j++;
        }
        // Add 1 to i and subtract 1 from j because the range is expanded by 1 on each side before it ends
        return s.substr(i+1, j-i-1);
    }
};
I solve the problem distinguishing two different cases.
First I consider the case when the length of the palindrome to be found is odd (there is a center). 
	I then expand the search to left and right from the possible found center.
Then I consider the case when the length of the palindrome to be found is pair (there is no center/middle).
	I then expand the search to left and right from the possible palindrome having the form "xx".
```/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    let longestP = s[0];
    let palindrome = "";
    if(s.length === 1 ) return s;
    
    //the length of the palindrome is odd
    for(let index = 1; index < s.length-1; index++){
        if(s[index - 1] === s[index + 1]){
            palindrome = s[index - 1] + s[index] + s[index + 1];
            for(let k = 1; index - 1 - k > -1 && index + 1 + k < s.length; k++){
                if(s[index - 1 - k] === s[index + 1 + k]){
                    palindrome = s[index - 1 - k] + palindrome + s[index + 1 + k];
                }
                else{
                    break;
                }
            }
        }
        if (palindrome.length > longestP.length){
            longestP = palindrome;
        }
        palindrome = "";
    }
    
    //the length of the palindrome is pair
    for(let index = 0; index < s.length-1; index++){
        if(s[index] === s[index + 1]){
            palindrome = s[index] + s[index + 1];
            for(let k = 1; (index - k > -1) && (index + 1 + k < s.length); k++){
                if(s[index - k] === s[index + 1 + k]){
                    palindrome = s[index - k] + palindrome + s[index + 1 + k];
                }
                else{
                    break;
                }
            }
        }
        if (palindrome.length > longestP.length){
            longestP = palindrome;
        }
        palindrome = "";
    }
    return longestP;
};`
Longest Palindromic Substring