#1876
Easy Algorithms Substrings of size three with distinct characters
Hash Table String Sliding Window Counting
76.6% acceptance
Feb 25, 2026
1683
57
A string is good if there are no repeated characters. Given a string s, return the number of good substrings of length three in s.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_good_substrings(s: String) -> i32 {
let b = s.as_bytes();
(0..b.len().saturating_sub(2))
.filter(|&i| b[i] != b[i+1] && b[i+1] != b[i+2] && b[i] != b[i+2])
.count() as i32
}
}