Skip to content

Commit 7d0b16e

Browse files
authored
Create 0387-first-unique-character-in-a-string.java
1 parent 34e0642 commit 7d0b16e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int firstUniqChar(String s) {
3+
Map<Character, Integer> count = new HashMap<>();
4+
for (Character ch : s.toCharArray()) {
5+
count.put(ch, count.getOrDefault(ch, 0) + 1);
6+
}
7+
for (int i = 0; i < s.length(); i++) {
8+
if (count.get(s.charAt(i)) == 1) {
9+
return i;
10+
}
11+
}
12+
return -1;
13+
}
14+
}

0 commit comments

Comments
 (0)