forked from ShashwatAwasthi04/Coding-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTack.txt
More file actions
41 lines (27 loc) · 748 Bytes
/
Copy pathSTack.txt
File metadata and controls
41 lines (27 loc) · 748 Bytes
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
//First non Repeating
public class Solution {
public String solve(String A) {
StringBuilder ans=new StringBuilder();
Queue<Character> q=new LinkedList<>();
int arr[]=new int[26];
for(int i=0;i<A.length();i++) {
char ch=A.charAt(i);
arr[ch-'a']++;
q.add(ch);
if(i==0 || arr[q.peek()-'a']==1) {
ans.append(q.peek());
}else {
while(!q.isEmpty()) {
if(arr[q.peek()-'a']!=1) {
q.poll();
}else {
break;
}
}
if(q.isEmpty()) ans.append('#');
else ans.append(q.peek());
}
}
return ans.toString();
}
}