LeetCode 387. First Unique Character in a String #83
quinnwencn
started this conversation in
General
Replies: 1 comment
-
AnalysisSolution written in CppSolution written in Rustimpl Solution {
pub fn first_uniq_char(s: String) -> i32 {
let mut alpha = [0; 26];
s.chars().for_each(|c| {
let index = c as usize - 'a' as usize;
alpha[index] += 1;
});
let mut i = 0;
for c in s.chars() {
let index = c as usize - 'a' as usize;
if alpha[index] == 1 {
return i;
}
i += 1;
}
-1
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Description
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.
Example 1:
Input: s = "leetcode"
Output: 0
Example 2:
Input: s = "loveleetcode"
Output: 2
Example 3:
Input: s = "aabb"
Output: -1
Constraints:
1 <= s.length <= 105
s consists of only lowercase English letters.
Beta Was this translation helpful? Give feedback.
All reactions