-
Notifications
You must be signed in to change notification settings - Fork 2
/
g. Non-repeating.cpp
48 lines (45 loc) · 1005 Bytes
/
g. Non-repeating.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Problem Link: https://practice.geeksforgeeks.org/problems/first-non-repeating-character-in-a-stream1216/1
Title: First non-repeating character in a stream
Difficulty: Medium
Author: Hariket Sukesh Kumar Sheth
Language: C++
*/
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
string FirstNonRepeating(string A){
// Code here
int t[26];
memset(t,0,sizeof t);
vector<char> first;
string res;
for(int i=0;i<A.size();i++){
if(t[A[i]-'a']==0) first.push_back(A[i]);
t[A[i]-'a']++;
int f=0, n=first.size();
for(int j=0;j<n;j++){
if(t[first[j]-'a']==1){
res+=first[j];
f=1;
break;
}
}
if(f==0) res+='#';
}
return res;
}
};
int main(){
int tc;
cin >> tc;
while(tc--){
string A;
cin >> A;
Solution obj;
string ans = obj.FirstNonRepeating(A);
cout << ans << "\n";
}
return 0;
}