<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:
    //Checks if index value is valid for string
    bool IsBoundsValid(string& string_to_check, int index_value)
    {
        return (index_value >= 0 && index_value <= string_to_check.length());
    }
    //Finds longest palindromic substring possible by expanding left and right from a given center of the string
    string PalindromeFromCenter(string& string_to_check, int left_ptr, int right_ptr)
    {
        int max_left_limit = -1;
        int max_right_limit = -1;
        while(IsBoundsValid(string_to_check, left_ptr)
            && IsBoundsValid(string_to_check, right_ptr)
            && string_to_check[left_ptr] == string_to_check[right_ptr])
        {
            max_left_limit = left_ptr--;
            max_right_limit = right_ptr++;
        }
        // if(IsBoundsValid(string_to_check, max_left_limit) && IsBoundsValid(string_to_check, max_right_limit))
        // {
        //     string palindrome = "";
        //     return string_to_check.substr(max_left_limit, max_right_limit - max_left_limit + 1);
        // }
        return string_to_check.substr(left_ptr + 1, right_ptr - left_ptr - 1);
    }
    //Returns the longer of two strings. If both strings are of same length returns the first one
    string GetLongerString(string& string_1, string& string_2)
    {
        return (string_1.length() >= string_2.length() ? string_1 : string_2);
    }
    //Finds the longest palindromic substring in s
    string longestPalindrome(string s) {
        //Initialise result with first chaaracter of s as it is given length of s > 0
        string longest_palindrome = "";
        longest_palindrome += s[0];

        for(int i=0; i<s.length(); i++)
        {
            string palindrome = PalindromeFromCenter(s, i-1, i+1);
            longest_palindrome = GetLongerString(longest_palindrome, palindrome);
            if(IsBoundsValid(s, i+1) && s[i] == s[i+1])
            {
                palindrome = PalindromeFromCenter(s, i, i+1);
                longest_palindrome = GetLongerString(longest_palindrome, palindrome);
            }
        }
        return longest_palindrome;
    }
};
53346