We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 62aa303 commit 47e2a5dCopy full SHA for 47e2a5d
src/0301-0400/387 - First Unique Character in a String/first_unique_char_in_a_string.rs
@@ -0,0 +1,21 @@
1
+impl Solution {
2
+ pub fn first_uniq_char(s: String) -> i32 {
3
+ let mut map = std::collections::HashMap::new();
4
+ for c in s.chars() {
5
+ // increase the count of the character if exists
6
+ // otherwise insert the character w/ default value 0
7
+ // then increase the count by 1
8
+ *map.entry(c).or_insert(0) += 1;
9
+ }
10
+
11
+ // iterate over the string and return the index of the first character
12
+ // that has a count of 1
13
+ for (i, c) in s.chars().enumerate() {
14
+ if *map.get(&c).unwrap() == 1 {
15
+ return i as i32;
16
17
18
19
+ -1
20
21
+}
0 commit comments