<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>

class Solution {
public:
    string longestPalindrome(string s) {
        int sum=0;
        vector<pair<int,string>> vp;
        for(int i=0;i<s.length();i++){
            int l=i;
            int length=0;
            string d="";
            for(int j=s.length()-1;j>=l;j--){
                if(s[j]==s[l] && j!=l){
                    l++;
                    length+=2;
                    d+=s[j];
                }
                else if(s[j]==s[l] && j==l){
                    length+=1;
                    d+=s[j];
                    l++;
                    break;
                }
                else if(length!=0 && s[j]!=s[l]){
                    j+=l-i;
                    length=0;
                    d.clear();
                    l=i;
                }
            }
            if(length==1){
                vp.push_back({length,d});
            }
            else if(length%2==0){
                string e=d;
                reverse(e.begin(),e.end());
                d+=e;
                vp.push_back({length,d});
            }
            else if(length%2!=0){
                string e=d.substr(0,length/2);
                reverse(e.begin(),e.end());
                d+=e;
                vp.push_back({length,d});
            }
        }
        sort(vp.begin(),vp.end());
        reverse(vp.begin(),vp.end());
        for(auto x:vp){
            cout<<x.second<<" ";
        }
        return vp[0].second;
    }
};
95670