#3884
Easy Algorithms First matching character from both ends
81.6% acceptance
Mar 31, 2026
26
0
You are given a string s of length n consisting of lowercase English letters.
Return the smallest index i such that s[i] == s[n - i - 1].
If no such index exists, return -1.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn first_matching_index(s: String) -> i32 {
let b = s.as_bytes();
let n = b.len();
for i in 0..n {
if b[i] == b[n - 1 - i] {
return i as i32;
}
}
-1
}
}