<p>Given a string <code>s</code>, return <em>the </em><span data-keyword="lexicographically-smaller-string"><em>lexicographically smallest</em></span> <span data-keyword="subsequence-string"><em>subsequence</em></span><em> of</em> <code>s</code> <em>that contains all the distinct characters of</em> <code>s</code> <em>exactly once</em>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;bcabc&quot;
<strong>Output:</strong> &quot;abc&quot;
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;cbacdcbc&quot;
<strong>Output:</strong> &quot;acdb&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> consists of lowercase English letters.</li>
</ul>

<p>&nbsp;</p>
<strong>Note:</strong> This question is the same as 316: <a href="https://leetcode.com/problems/remove-duplicate-letters/" target="_blank">https://leetcode.com/problems/remove-duplicate-letters/</a>
class Solution {
public:
    string smallestSubsequence(string s) {
     stack<char>st;
     int arr[26]={0};

     for(int i=0; i<s.size(); i++){
        arr[s[i]-'a']=i;
     }  
     vector<bool>present(26, false);
     st.push(s[0]);
     present[s[0]-'a']=true;
     for(int i=1; i<s.size(); i++){
        if(present[s[i]-'a']==true) continue;
            while(!st.empty() && s[i]<st.top() && i<arr[st.top()-'a']){
                present[st.top()-'a']=false;
                 st.pop();
            }

            st.push(s[i]);
            present[s[i]-'a']=true;
     }
     string str="";
     while(!st.empty()){
         str+=st.top();
         st.pop();
     }
    
     reverse(str.begin(), str.end());
     return str;
    }
};
7900