#3083
Easy Algorithms Existence of a substring in a string and its reverse
Hash Table String
66.4% acceptance
Feb 25, 2026
111
1
Given a string s, find any substring of length 2 which is also present in the reverse of s.
Return true if such a substring exists, and false otherwise.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn is_substring_present(s: String) -> bool {
let b = s.as_bytes();
let rev: Vec<u8> = b.iter().rev().cloned().collect();
b.windows(2).any(|w| rev.windows(2).any(|r| r == w))
}
}