fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. char getValid(unordered_map<char,int> mp, deque<char> dq) {
  5. while(!dq.empty()) {
  6. char curr = dq.front();
  7. dq.pop_front();
  8. if(mp[curr] >1) continue;
  9. return curr;
  10. }
  11. return '#';
  12. }
  13.  
  14. string solve(string &str) {
  15. deque<char> dq;
  16. unordered_set<char> sett;
  17.  
  18. string ans="";
  19. char curr = '0';
  20.  
  21. unordered_map<char,int> mp;
  22. int i=0;
  23. // A = "abcabc", output: "aaabc#"
  24. for(auto ch:str) {
  25.  
  26. if(mp.find(ch) == mp.end()) {
  27. dq.push_back(ch);
  28. }
  29. if(i==0) {
  30. curr = getValid(mp,dq);
  31. i++;
  32. }
  33.  
  34. mp[ch]++;
  35. if(mp[curr] <= 1) {
  36. ans += curr;
  37. }else {
  38. curr =getValid(mp,dq);
  39. ans+=curr;
  40. }
  41. }
  42. return ans;
  43.  
  44. }
  45.  
  46. int main() {
  47. string str = "abba";
  48. cout<<solve(str)<<endl;
  49. return 0;
  50. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
aaa#