Simple 12 line CPP code O(n) time


#1

string Solution::solve(string A) {
queue q;
vector f(26,false);
vector s(26,false);
string res;
for(int i=0;i<A.length();i++){
if(f[A[i]-‘a’]) s[A[i]-‘a’] = true;
else f[A[i] - ‘a’] = true;
q.push(A[i]);
while(!q.empty() && s[q.front()-‘a’]) q.pop();
res += q.empty() ? ‘#’ : q.front();
}
return res;
}