#2083
Medium Algorithms Substrings that begin and end with the same letter
Hash Table Math String Counting Prefix Sum
74.4% acceptance
Mar 31, 2026
137
12
No description available.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn number_of_substrings(s: String) -> i64 {
let mut count = [0i64; 26];
let mut ans: i64 = 0;
for b in s.bytes() {
let idx = (b - b'a') as usize;
count[idx] += 1;
ans += count[idx];
}
ans
}
}