-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique.java
More file actions
37 lines (27 loc) · 752 Bytes
/
Unique.java
File metadata and controls
37 lines (27 loc) · 752 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
class Unique {
public int firstUniqChar(String s) {
int i=0;
int size=s.length();
int prev;
int newk;
LinkedHashMap<Character,Integer> map=new LinkedHashMap<Character,Integer>();
for(i=0;i<size;i++){
char ch =s.charAt(i);
if(map.containsKey(ch)){
prev=map.get(ch);
newk=prev +1;
map.replace(ch,newk);}
else
map.put(ch,1);
}
int k=-1;
for(Integer a : map.values()){
k=k+a-1;
if(a==1)
break;
}
if(k==(size+1))
k=-1;
return k;
}
}