Easy to understand solution in c++


#1

string Solution::solve(string A) {

unordered_map<char,int> mp;
unordered_map<char,int> mp1;
for(int i = 0;i<A.length();i++)
{
    mp[A[i]]++;
}
stack<char> s;
s.push(A[0]);
mp[A[0]]--;
mp1[A[0]]++;
for(int i = 1;i<A.length();i++)
{
    if(s.top() < A[i] && mp1.count(A[i]) == 0)
    {
        mp[A[i]]--;
        s.push(A[i]);
        mp1[A[i]]++;
    }
    else if(mp1.count(A[i]) == 0)
    {
        while(!s.empty() && mp[s.top()] > 0 && s.top() >= A[i])
        {
            mp1.erase(s.top());
            s.pop();
        }
        s.push(A[i]);
        mp[A[i]]--;
        mp1[A[i]]++;
    }
    else
    {
        mp[A[i]]--;
    }
}
string s11 = "";
while(!s.empty())
{
    s11 += s.top();
    s.pop();
}
reverse(s11.begin(),s11.end());
return s11;

}