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

/*
So The first thing that comes to my mind is-
Can have 2 pointers starting from the 0 index going until n-1 increasing by 2 everytime 
(can't be coz there can be a palindrome of even or odd size so I'll increase by 1 only)
j starts from i + 1 everytime---->  i goes until n-2
Soo this is just brute force, but I have an Idea, I can keep an hashmap which simply keeps a
track of the count of each character, if they are even only then I'll check else no 
 and there's
one more condition, I'll also keep a counter to count the number of chars having count 1 coz
that can only be either 0 or 1 (think) and if it's greater than that then I aint checking
Updated: It would be count of characters having odd count...
And I guess both of these conditions just can be solved by using the counter variable I 
mentioned above

unordered_map<char, int> charCount;
int charHavingOddCount = 0;

void increaseCharCount(char c){
    if(!charCount.count(c)) {
        charHavingOddCount++;
    }
    else if(charCount[c] % 2){
         charHavingOddCount--;
    }
    charCount[c]++;
}

bool checkPalindrome(string& pali){
    int n = pali.size();
    for(int i = 0; i <= n/2; i++){
        if(pali[i] != pali[n-i-1]) return false;
    }
    return true;
}

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.size() <=1) return s;
        string ans = "";
        ans += s[0];
        for(int i =0; i < s.size()-1; i++){
            increaseCharCount(s[i]);
            for(int j = i+1; j < s.size(); j++){
                increaseCharCount(s[j]);
                if(!(charHavingOddCount >= 1)){
                    string k = s.substr(i, j-i+1);
                    if(k.size() > ans.size()) {
                        bool flag = checkPalindrome(k);
                        if(flag) ans = k;
                    }
                }
            }
        }
        return ans;
    }
};

One good condition check I got after I got TLE from the above soln T_T:
Keep increasing j pointer until it j's char is not equal to i's char

*/

bool checkPalindrome(string& pali){
    int n = pali.size();
    for(int i = 0; i <= n/2; i++){
        if(pali[i] != pali[n-i-1]) return false;
    }
    return true;
}

class Solution {
public:
    string longestPalindrome(string s) {
        if(s.size() <=1) return s;
        string ans = "";
        int i =0;
        ans += s[0];
        while(i < s.size()-1){
            int j = (1 > ans.size()-1) ? i+1 : i + ans.size()-1;
            while(j < s.size()){
                while(j < s.size() && s[i] != s[j]) j++;
                string k = s.substr(i, j-i+1);
                bool flag = checkPalindrome(k);
                if(flag) ans = k;
                j++;
            }
            i++;
        }
        return ans;
    }
};
361705