-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonRepeatingCharacter.java
56 lines (45 loc) · 1.36 KB
/
nonRepeatingCharacter.java
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
49
50
51
52
53
54
55
56
/*
Input
The first line contains T denoting the number of testcases. Then follows description of testcases.
Each case begins with a single integer N denoting the length of string. The next line contains the string s.
Output
For each testcase, print the first non repeating character present in string.
Print -1 if there is no non repeating character. */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int q,t;
t=scan.nextInt();
for(q=0;q<t;q++)
{
int i,n;
n = scan.nextInt();
String s = scan.next();
char ans='1';
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(i=0;i<n;i++)
{
if(map.containsKey(s.charAt(i)))
{
int count = map.get(s.charAt(i)) + 1;
map.put(s.charAt(i),count);
}
else
map.put(s.charAt(i),1);
}
for(i=0;i<n;i++)
if(map.containsKey(s.charAt(i)) && map.get(s.charAt(i))==1)
{
ans = s.charAt(i);
break;
}
if(ans!='1')
System.out.println(ans);
else
System.out.println(-1);
}
}
}