<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:
    bool isPalindrome(string& temp, size_t length)
    {
        for(int i = 0; i < length/2 + 1; ++i)
        {
            if(temp[i] != temp[length - i - 1])
                return false;
        }
        return true;
    }

    string longestPalindrome(string s) {
        // not sure exactly if what I am thinking is dynamic programming, but,
        // 1 way to do this is, start from index 0, and then keep on going and from back, start checking 
        // if paindrome, but that is very tedious, what else

        int length = s.length();
        map<int, string> mapp;

        // right now odd number one palindrome
        int left = 0, right = 0, center = 0;
        for(; center < length; ++center)
        {
            int sublength = 3;
            for(left = center - 1, right = center + 1; left >= 0 && right <= length - 1; left--, right++)
            {
                if(s[left] != s[right])
                    break;
                else
                {
                    if(mapp.find(sublength) == mapp.end())
                    {
                        mapp[sublength] = s.substr(left, right - left + 1);
                    }
                }
                sublength += 2;
            }
        }

        // for even now
        for(center = 0; center < length - 1; ++center)
        {
            int sublength = 2;
            for(left = center, right = center + 1; left >= 0 && right <= length - 1; left--, right++)
            {
                if(s[left] != s[right])
                    break;
                else
                {
                    if(mapp.find(sublength) == mapp.end())
                    {
                        mapp[sublength] = s.substr(left, right - left + 1);
                    }
                }
                sublength += 2;
            }
        }

        if(mapp.size() == 0)
            return string(1, s[0]);
        
        auto itr = prev(mapp.end());
        return itr->second;
    }
};
29161