-
Notifications
You must be signed in to change notification settings - Fork 0
/
First_Unique.java
54 lines (51 loc) · 1.65 KB
/
First_Unique.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
//387. First Unique Character in a String
import java.util.HashMap;
import java.util.Map;
public class First_Unique {
public static void main(String[] args) {
String s = "leeolttcocded";
System.out.println(firstUniqChar(s));
}
// public static int firstUniqChar(String s) {
// String []str = s.split("");
// int n = str.length;
// HashMap<String,Integer> count = new HashMap<>();
// HashMap<String,Integer> index = new HashMap<>();
// for(int i = 0;i<n;i++){
// count.put(str[i],count.getOrDefault(str[i],0)+1);
// index.put(str[i],i);
// }
// int min = Integer.MAX_VALUE;
// boolean b= false;
// for(Map.Entry<String,Integer> entry : count.entrySet()){
// if(entry.getValue() == 1){
// min = Math.min(min,index.get(entry.getKey()));
// b = true;
// }
// }
// if(b==false){
// return -1;
// }
// return min;
// }
// optimised
public static int firstUniqChar(String s) {
int n = s.length();
HashMap<Character,Integer> count = new HashMap<>();
for(int i = 0;i<n;i++){
count.put(s.charAt(i),count.getOrDefault(s.charAt(i),0)+1);
}
int min = Integer.MAX_VALUE;
boolean b= false;
for(Map.Entry<Character,Integer> entry : count.entrySet()){
if(entry.getValue() == 1){
min = Math.min(min,s.indexOf(entry.getKey()));
b = true;
}
}
if(b==false){
return -1;
}
return min;
}
}