#2351
Easy Algorithms First letter to appear twice
Hash Table String Bit Manipulation Counting
74.9% acceptance
Feb 25, 2026
1182
65
Given a string s consisting of lowercase English letters, return the first letter to appear twice.
Note:
A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.
s will contain at least one letter that appears twice.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn repeated_character(s: String) -> char {
let mut seen = 0u32;
for c in s.chars() {
let bit = 1u32 << (c as u8 - b'a');
if seen & bit != 0 { return c; }
seen |= bit;
}
unreachable!()
}
}