Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include<bits/stdc++.h>
- using namespace std;
- // } Driver Code Ends
- class Solution {
- public:
- string FirstNonRepeating(string A){
- // Code here
- queue<int> q;
- q.push(A[0]);
- unordered_map<char,int> m;
- m[A[0]]++;
- int i=1;
- string ans="";
- ans+=A[0];
- while(i<A.length())
- {
- m[A[i]]++;
- q.push(A[i]);
- while(m[q.front()]>1 && !q.empty())
- {
- q.pop();
- }
- if(!q.empty())
- {
- ans+=q.front();
- }
- else
- {
- ans+='#';
- }
- i++;
- }
- return ans;
- }
- };
- //{ Driver Code Starts.
- int main(){
- int tc;
- cin >> tc;
- while(tc--){
- string A;
- cin >> A;
- Solution obj;
- string ans = obj.FirstNonRepeating(A);
- cout << ans << "\n";
- }
- return 0;
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment