fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // } Driver Code Ends
  5. class Solution {
  6. public:
  7. string FirstNonRepeating(string A){
  8. map<char, int> m;
  9. string ans = "";
  10. queue<char> q;
  11.  
  12. for(int i=0; i<A.length(); i++) {
  13. char ch = A[i];
  14.  
  15. q.push(ch);
  16. m[ch]++;
  17.  
  18. while(!q.empty()) {
  19. if(m[q.front()] > 1){
  20. q.pop();
  21. }
  22. else
  23. {
  24. ans.push_back(q.front());
  25. break;
  26. }
  27. }
  28. if(q.empty()){
  29. ans.push_back('#');
  30. }
  31. }
  32. return ans;
  33. }
  34.  
  35. };
  36.  
  37. // { Driver Code Starts.
  38. int main(){
  39. int tc;
  40. cin >> tc;
  41. while(tc--){
  42. string A;
  43. cin >> A;
  44. Solution obj;
  45. string ans = obj.FirstNonRepeating(A);
  46. cout << ans << "\n";
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout