-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstUniqueChar.java
More file actions
executable file
·42 lines (40 loc) · 1.16 KB
/
FirstUniqueChar.java
File metadata and controls
executable file
·42 lines (40 loc) · 1.16 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
import java.util.*;
public class FirstUniqueChar {
public int firstUniqChar(String s) {
LinkedHashMap<Character, Integer> hm = new LinkedHashMap<>();
for (int i = 0; i < s.length(); i++) {
if (!hm.containsKey(s.charAt(i))) {
hm.put(s.charAt(i), 1);
} else {
int a = hm.get(s.charAt(i));
hm.put(s.charAt(i), a+1);
}
}
char temp=' ';
for (char x : hm.keySet()) {
if (hm.get(x) == 1) {
temp = x;
break;
}
}
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)==temp)
return i;
}
return -1;
}
public static void main(String[] args) {
FirstUniqueChar f = new FirstUniqueChar();
int x = f.firstUniqChar("leetcodel");
System.out.println(x);
}
}