387. First Unique Character in a String
Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.
Examples:
s = "leetcode" return 0. s = "loveleetcode" return 2.
Note: You may assume the string contains only lowercase English letters.
Rust Solution
struct Solution;
use std::collections::HashMap;
impl Solution {
fn first_uniq_char(s: String) -> i32 {
let mut hm: HashMap<char, i32> = HashMap::new();
for c in s.chars() {
let e = hm.entry(c).or_default();
*e += 1;
}
for (i, c) in s.chars().enumerate() {
if let Some(1) = hm.get(&c) {
return i as i32;
}
}
-1
}
}
#[test]
fn test() {
assert_eq!(Solution::first_uniq_char("leetcode".to_string()), 0);
assert_eq!(Solution::first_uniq_char("loveleetcode".to_string()), 2);
}
Having problems with this solution? Click here to submit an issue on github.