#include <bits/stdc++.h>
using namespace std;
char getValid(unordered_map<char,int> mp, deque<char> dq) {
while(!dq.empty()) {
char curr = dq.front();
dq.pop_front();
if(mp[curr] >1) continue;
return curr;
}
return '#';
}
string solve(string &str) {
deque<char> dq;
unordered_set<char> sett;
string ans="";
char curr = '0';
unordered_map<char,int> mp;
int i=0;
// A = "abcabc", output: "aaabc#"
for(auto ch:str) {
if(mp.find(ch) == mp.end()) {
dq.push_back(ch);
}
if(i==0) {
curr = getValid(mp,dq);
i++;
}
mp[ch]++;
if(mp[curr] <= 1) {
ans += curr;
}else {
curr =getValid(mp,dq);
ans+=curr;
}
}
return ans;
}
int main() {
string str = "abba";
cout<<solve(str)<<endl;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjaGFyIGdldFZhbGlkKHVub3JkZXJlZF9tYXA8Y2hhcixpbnQ+IG1wLCBkZXF1ZTxjaGFyPiBkcSkgewoJd2hpbGUoIWRxLmVtcHR5KCkpIHsKCQljaGFyIGN1cnIgPSBkcS5mcm9udCgpOwoJCWRxLnBvcF9mcm9udCgpOwoJCWlmKG1wW2N1cnJdID4xKSBjb250aW51ZTsKCQlyZXR1cm4gY3VycjsKCX0KCXJldHVybiAnIyc7Cn0KCnN0cmluZyBzb2x2ZShzdHJpbmcgJnN0cikgewoJZGVxdWU8Y2hhcj4gZHE7Cgl1bm9yZGVyZWRfc2V0PGNoYXI+IHNldHQ7CgkKCXN0cmluZyBhbnM9IiI7CgljaGFyIGN1cnIgPSAnMCc7CgkKCXVub3JkZXJlZF9tYXA8Y2hhcixpbnQ+IG1wOwoJaW50IGk9MDsKCS8vIEEgPSAiYWJjYWJjIiwgb3V0cHV0OiAiYWFhYmMjIgoJZm9yKGF1dG8gY2g6c3RyKSB7CgkKCQlpZihtcC5maW5kKGNoKSA9PSBtcC5lbmQoKSkgewoJCQlkcS5wdXNoX2JhY2soY2gpOwoJCX0KCQkJaWYoaT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