Skip to main content
Back to problems
#387
Easy Algorithms

First unique character in a string

Hash Table String Queue Counting
65.1% acceptance
Jan 12, 2026
9742
333
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn first_uniq_char(s: String) -> i32 {
    let mut counts = [0; 26];
    
    for b in s.bytes() {
      counts[(b - b'a') as usize] += 1;
    }
    
    for (i, b) in s.bytes().enumerate() {
      if counts[(b - b'a') as usize] == 1 {
        return i as i32;
      }
    }
    
    -1
  }
}