<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) {
        // check for bestcases, s already palindrome itself
        string resultMaxPalindrome = s;
        reverse(resultMaxPalindrome.begin(), resultMaxPalindrome.end());

        if(s == resultMaxPalindrome) {
            return s;
        }

        // if not best cases, then run normal flow
        resultMaxPalindrome = "";

        for(int i = 0; i < s.length() - 1; i++) { //check from left
            bool isPalindromeFound = false;

            for(int j = s.length()-1; j > i && !isPalindromeFound && ((j-i+1) > resultMaxPalindrome.length()); j--) { //check from right
                if(s[i] == s[j]) {
                    //start check palindrome
                    int indexLeft = i, indexRight = j;
                    string leftSidePalindrome = "", rightSidePalindrome = "", tempResultMaxPalindrome = "";

                    while(s[indexLeft] == s[indexRight] && indexLeft < indexRight) {
                        leftSidePalindrome += s[indexLeft];
                        rightSidePalindrome += s[indexRight];
                        indexLeft++;
                        indexRight--;
                    }

                    if(indexLeft == indexRight || indexLeft == indexRight+1) {
                        //palindrome
                        isPalindromeFound = true;

                        if(indexLeft == indexRight) { //middle
                            leftSidePalindrome += s[indexLeft];
                        }

                        reverse(rightSidePalindrome.begin(), rightSidePalindrome.end());
                        tempResultMaxPalindrome = leftSidePalindrome + rightSidePalindrome;

                        if(tempResultMaxPalindrome.length() > resultMaxPalindrome.length()) {
                            resultMaxPalindrome = tempResultMaxPalindrome;
                        }
                    }
                }
            }

            if(isPalindromeFound && resultMaxPalindrome.length() == s.length() - 1) {
                //already found max palindrome
                break;
            }
        }

        if(resultMaxPalindrome == "") {
            resultMaxPalindrome += s[0];
        }

        return resultMaxPalindrome;
    }
};
277058