First Unique Character in a String LeetCode solution in Java

1
class Solution {
2
public int firstUniqChar(String s) {
3
HashMap<Character, Integer> hmap = new HashMap<>();
4
for (int i = 0; i < s.length(); i++) {
5
if (!hmap.containsKey(s.charAt(i))) {
6
hmap.put(s.charAt(i), 1);
7
} else {
8
hmap.put(s.charAt(i), hmap.get(s.charAt(i)) + 1);
9
}
10
}
11
for (int i = 0; i < s.length(); i++) {
12
if (hmap.get(s.charAt(i)) == 1) return i;
13
}
14
return -1;
15
}
16
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0