-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst_occurence.java
More file actions
38 lines (36 loc) · 952 Bytes
/
first_occurence.java
File metadata and controls
38 lines (36 loc) · 952 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
32
33
34
35
36
37
38
//First Occurence of the Non-Repeating Character
//Optimized Approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class first_occurence
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t--!=0)
{
String str=sc.next();
int freq[]=new int[26];
int flag=0;
for(int i=0;i<str.length();i++)
{
int a=freq[str.charAt(i)-97]++;
}
for(int i=0;i<str.length();i++)
{
if(freq[str.charAt(i)-97]==1)
{
System.out.println(str.charAt(i));
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(-1);
}
}
}
}