Skip to content

Commit 47e2a5d

Browse files
committed
solved: 387. First Unique Character in a String
Signed-off-by: rajput-hemant <[email protected]>
1 parent 62aa303 commit 47e2a5d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)