<p>Given a string <code>s</code>, return <em>the longest</em> <span data-keyword="palindromic-string"><em>palindromic</em></span> <span data-keyword="substring-nonempty"><em>substring</em></span> in <code>s</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;babad&quot;
<strong>Output:</strong> &quot;bab&quot;
<strong>Explanation:</strong> &quot;aba&quot; is also a valid answer.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;cbbd&quot;
<strong>Output:</strong> &quot;bb&quot;
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 1000</code></li>
	<li><code>s</code> consist of only digits and English letters.</li>
</ul>

bool isPalindrome(string &s, int start, int end){
    while(s[start] == s[end]){
        if( start > end ) return true;
        start++;
        end--;
    }    
    
    return false;
}

string findPalindrome(string &s, int **arr){
    int i = 0;
    int len = s.size();
    int j = len-1;
    int pal = 0;
    int size =0;
    for(int k =0; k < len; k++){
        pal = arr[i][j];
        size = j-i+1;

        if( pal == size){
            return s.substr(i, size);
        }    
        if(pal == arr[i+1][j]){
            i++;
        }
        else{
            j--;
        }
    }
    
    return s;
}

class Solution{
    public:
    string longestPalindrome(string s){
        int len = s.size();
        int start = 0;
        int k = 1;
        int **arr;
        arr= new int*[len];

        for(int i =0; i< len; i++){
            arr[i] = new int[len];
        }

        for(int i =0; i< s.size(); i++){
            arr[i][i] = 1;
        }
        
        for(int j = len -1; j> 0; j--){
            for(int i =0; i < j; i++){
                if(isPalindrome(s, i, i+k)){
                    arr[i][i+k] = k+1;
                }
                else{
                    arr[i][i+k] = std::max(arr[i+1][i+k], arr[i][i+k-1]);
                }
            }
            k++;
        }
        
        return findPalindrome(s, arr);
    }
};
289150