<p>Given a string <code>s</code> of lowercase letters, you need to find the maximum number of <strong>non-empty</strong> substrings of <code>s</code> that meet the following conditions:</p>

<ol>
	<li>The substrings do not overlap, that is for any two substrings <code>s[i..j]</code> and <code>s[x..y]</code>, either <code>j &lt; x</code> or <code>i &gt; y</code> is true.</li>
	<li>A substring that contains a certain character <code>c</code> must also contain all occurrences of <code>c</code>.</li>
</ol>

<p>Find <em>the maximum number of substrings that meet the above conditions</em>. If there are multiple solutions with the same number of substrings, <em>return the one with minimum total length. </em>It can be shown that there exists a unique solution of minimum total length.</p>

<p>Notice that you can return the substrings in <strong>any</strong> order.</p>

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

<pre>
<strong>Input:</strong> s = &quot;adefaddaccc&quot;
<strong>Output:</strong> [&quot;e&quot;,&quot;f&quot;,&quot;ccc&quot;]
<b>Explanation:</b>&nbsp;The following are all the possible substrings that meet the conditions:
[
&nbsp; &quot;adefaddaccc&quot;
&nbsp; &quot;adefadda&quot;,
&nbsp; &quot;ef&quot;,
&nbsp; &quot;e&quot;,
  &quot;f&quot;,
&nbsp; &quot;ccc&quot;,
]
If we choose the first string, we cannot choose anything else and we&#39;d get only 1. If we choose &quot;adefadda&quot;, we are left with &quot;ccc&quot; which is the only one that doesn&#39;t overlap, thus obtaining 2 substrings. Notice also, that it&#39;s not optimal to choose &quot;ef&quot; since it can be split into two. Therefore, the optimal way is to choose [&quot;e&quot;,&quot;f&quot;,&quot;ccc&quot;] which gives us 3 substrings. No other solution of the same number of substrings exist.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abbaccd&quot;
<strong>Output:</strong> [&quot;d&quot;,&quot;bb&quot;,&quot;cc&quot;]
<b>Explanation: </b>Notice that while the set of substrings [&quot;d&quot;,&quot;abba&quot;,&quot;cc&quot;] also has length 3, it&#39;s considered incorrect since it has larger total length.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> contains only lowercase English letters.</li>
</ul>

class Solution {
public:

    int findRight(int i, string s, vector<int> &begin, vector<int> &end){
        int rightEnd=end[s[i]-'a'];
        for(int j = i; j<=rightEnd; j++){// sees every character from i to rightEnd
            if(begin[s[j]-'a'] < i) return -1; // checks kisi char ka start i se peeche toh nahi hai
            rightEnd = max(rightEnd, end[s[j]-'a']);
        }
        return rightEnd;
    }


    vector<string> maxNumOfSubstrings(string s) {
        int n = s.size();
        vector<int> begin(26,INT_MAX);
        vector<int> end(26,INT_MIN);
        vector<string> res;

        // determining 1st and last index of each character in string s
        for(int i =0; i<s.size(); i++){
            begin[s[i]-'a'] = min(begin[s[i]-'a'],i);
            end[s[i]-'a'] = max(end[s[i]-'a'], i);
        }

        // now start traversing on string to find non-overlapping substrings
        int prev = -1; // where did last substring end
        for(int i =0; i<s.size(); i++){
            if(i == begin[s[i]-'a']){ // agar yahi se string banana chalu hua hai, ie first 
                                      // occurance of character
                int rightEnd = findRight(i,s,begin,end); // finds right end and checks for 
                                                         // validity of substring starting from ind i

                if(rightEnd==-1) continue; // invalid substring so skip it

                if(i <= prev){ // matlab ki mujhe ek aisa substr mila jo overlap karra hai pichle
                               // vale substr se toh hum ek naya string hi daal dete hain
                    res.back() = s.substr(i, rightEnd-i+1);
                }
                else{ // i > prev ka matlab hai ki mujhe naya non-overlapping substr mila hai
                    res.push_back(s.substr(i, rightEnd-i+1));
                }
                prev = rightEnd;

            }
        }
        return res;
    }
};
55635