<p>Given a string <code>s</code>. In one step you can insert any character at any index of the string.</p>

<p>Return <em>the minimum number of steps</em> to make <code>s</code>&nbsp;palindrome.</p>

<p>A&nbsp;<b>Palindrome String</b>&nbsp;is one that reads the same backward as well as forward.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;zzazz&quot;
<strong>Output:</strong> 0
<strong>Explanation:</strong> The string &quot;zzazz&quot; is already palindrome we do not need any insertions.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;mbadm&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> String can be &quot;mbdadbm&quot; or &quot;mdbabdm&quot;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;leetcode&quot;
<strong>Output:</strong> 5
<strong>Explanation:</strong> Inserting 5 characters the string becomes &quot;leetcodocteel&quot;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 500</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>



class Solution {
public:
    int minInsertions(string s) {
        // Idea :
        // Divide the string in two halves, check LCS.

        int ns = s.size();

        // string s1 = s.substr(0, ns/2);
        string s2 = s;

        reverse(s2.begin(), s2.end());

        // cout << s1 << " " << s2;
        // LIS begins
        // int m = s1.size();
        int n = s2.size();

        unordered_map<char, vector<int>> map;

        for(int i=0; i<n; i++){
            map[s2[i]].push_back(i);
        }

        vector<int> seq;

        for(auto c : s){
            if(map.find(c) != map.end()){
                for(auto it = map[c].rbegin(); it != map[c].rend(); it++){
                    seq.push_back(*it);
                }
            }
        }

        int lislen = LISlen(seq);
        // cout << seq.size();
        if(seq.size() == 0){
            return 0;
        }
        
        // for(int p = 0; p<seq.size(); p++){
        //     cout << p << endl;
        // }

        int insertions = ns-lislen;

        return insertions;


    }

private:
    int LISlen(const vector<int> seq){
        vector<int> lis;

        for(int i : seq){
            auto it = lower_bound(lis.begin(), lis.end(), i);

            if(it == lis.end()){
                lis.push_back(i);
            }
            else{
                *it = i;
            }
        }

        return lis.size();
    }
};
14500