-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathFirstUniqueCharacterInString387.java
50 lines (39 loc) · 1.03 KB
/
FirstUniqueCharacterInString387.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
package easy;
import java.util.HashMap;
public class FirstUniqueCharacterInString387 {
// O(N) Time
public static int firstUniqChar1(String s) {
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (!map.containsKey(ch)) {
map.put(ch, 1);
} else {
map.put(ch, map.get(ch) + 1);
}
}
for (int i = 0; i < s.length(); i++) {
if (map.get(s.charAt(i)) == 1)
return i;
}
return -1;
}
public static int firstUniqChar(String s) {
int[] count = new int[26]; // for all alphabets
char[] arr = s.toCharArray();
for (int i = 0; i < arr.length; i++) {
count[arr[i] - 'a']++; // track count of each character in String
}
for (int i = 0; i < arr.length; i++) {
if (count[arr[i] - 'a'] == 1)
return i;
}
return -1;
}
public static void main(String[] args) {
String str = "leetcode";
System.out.println(firstUniqChar(str));
System.out.println(firstUniqChar("loveleetcode"));
System.out.println(firstUniqChar("aabb"));
}
}