<p>You are given a string <code>s</code>. We want to partition the string into as many parts as possible so that each letter appears in at most one part.</p>

<p>Note that the partition is done so that after concatenating all the parts in order, the resultant string should be <code>s</code>.</p>

<p>Return <em>a list of integers representing the size of these parts</em>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;ababcbacadefegdehijhklij&quot;
<strong>Output:</strong> [9,7,8]
<strong>Explanation:</strong>
The partition is &quot;ababcbaca&quot;, &quot;defegde&quot;, &quot;hijhklij&quot;.
This is a partition so that each letter appears in at most one part.
A partition like &quot;ababcbacadefegde&quot;, &quot;hijhklij&quot; is incorrect, because it splits s into less parts.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;eccbbbbdec&quot;
<strong>Output:</strong> [10]
</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:
    vector<int> partitionLabels(string s) {
        int n = s.size();
        map<char, pair<int, int>>m1;
        for(char ch = 'a'; ch <= 'z'; ++ch){
            m1[ch] = {INT_MAX, INT_MIN};
        }
        for(int i = 0; i < s.size(); ++i){
            m1[s[i]].first = min(m1[s[i]].first, i);
            m1[s[i]].second = max(m1[s[i]].second, i);
        }
        
        vector<pair<int, int>>v;
        for(auto x : m1){
            if(x.second.first != INT_MAX && x.second.second != INT_MIN)
               v.push_back(x.second);
        }
        
        sort(v.begin(), v.end());
        
        pair<int, int>p = v[0];
        vector<pair<int, int>>ans;
        for(int i = 1; i < v.size(); ++i){
            if(v[i].first <= p.second){
                p.second = max(v[i].second, p.second);
                p.first = min(p.first, v[i].first);
            } else {
                ans.push_back(p);
                p = v[i];
            }
        }
        
        ans.push_back(p);
        vector<int> ans1;
        for(auto x : ans){
           ans1.push_back(x.second-x.first+1);
        }
        
        return ans1;
        
    }
};
10000