forked from imnishant/GeeksforGeeks-Java-Solution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst-non-repeating-character-in-a-stream
More file actions
43 lines (38 loc) · 1.09 KB
/
first-non-repeating-character-in-a-stream
File metadata and controls
43 lines (38 loc) · 1.09 KB
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws Exception
{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
while(t-- > 0)
{
int n = Integer.parseInt(bf.readLine());
String s = bf.readLine().trim();
int freq[] = new int[26];
Queue<Character> q = new LinkedList<Character>();
StringBuffer sb = new StringBuffer();
for(int i=0 ; i<n ; i++)
{
char ch = s.charAt(i*2);
freq[ch - 'a']++;
q.add(ch);
while(!q.isEmpty())
{
if(freq[q.peek() - 'a'] > 1)
q.poll();
else
{
sb.append(q.peek() + " ");
break;
}
}
if(q.isEmpty())
sb.append(-1 + " ");
}
System.out.println(sb);
}
}
}