Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Build confidence with an intuition-first walkthrough focused on hash map fundamentals.
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.
Example 1:
Input: s = "leetcode"
Output: true
Explanation: Substring "ee" is of length 2 which is also present in reverse(s) == "edocteel".
Example 2:
Input: s = "abcba"
Output: true
Explanation: All of the substrings of length 2 "ab", "bc", "cb", "ba" are also present in reverse(s) == "abcba".
Example 3:
Input: s = "abcd"
Output: false
Explanation: There is no substring of length 2 in s, which is also present in the reverse of s.
Constraints:
1 <= s.length <= 100s consists only of lowercase English letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"leetcode"
"abcba"
"abcd"
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3083: Existence of a Substring in a String and Its Reverse
class Solution {
public boolean isSubstringPresent(String s) {
boolean[][] st = new boolean[26][26];
int n = s.length();
for (int i = 0; i < n - 1; ++i) {
st[s.charAt(i + 1) - 'a'][s.charAt(i) - 'a'] = true;
}
for (int i = 0; i < n - 1; ++i) {
if (st[s.charAt(i) - 'a'][s.charAt(i + 1) - 'a']) {
return true;
}
}
return false;
}
}
// Accepted solution for LeetCode #3083: Existence of a Substring in a String and Its Reverse
func isSubstringPresent(s string) bool {
st := [26][26]bool{}
for i := 0; i < len(s)-1; i++ {
st[s[i+1]-'a'][s[i]-'a'] = true
}
for i := 0; i < len(s)-1; i++ {
if st[s[i]-'a'][s[i+1]-'a'] {
return true
}
}
return false
}
# Accepted solution for LeetCode #3083: Existence of a Substring in a String and Its Reverse
class Solution:
def isSubstringPresent(self, s: str) -> bool:
st = {(a, b) for a, b in pairwise(s[::-1])}
return any((a, b) in st for a, b in pairwise(s))
// Accepted solution for LeetCode #3083: Existence of a Substring in a String and Its Reverse
fn is_substring_present(s: String) -> bool {
let cs1: Vec<char> = s.chars().collect();
let cs2: Vec<char> = s.chars().rev().collect();
let cs1: Vec<_> = cs1.windows(2).collect();
let cs2: Vec<_> = cs2.windows(2).collect();
for &s1 in &cs1 {
for &s2 in &cs2 {
if *s1 == *s2 {
return true;
}
}
}
false
}
fn main() {
println!("ret={}", is_substring_present("leetcode".to_string()));
}
#[test]
fn test() {
assert!(is_substring_present("leetcode".to_string()));
assert!(is_substring_present("abcba".to_string()));
assert!(!is_substring_present("abcd".to_string()));
}
// Accepted solution for LeetCode #3083: Existence of a Substring in a String and Its Reverse
function isSubstringPresent(s: string): boolean {
const st: boolean[][] = Array.from({ length: 26 }, () => Array(26).fill(false));
for (let i = 0; i < s.length - 1; ++i) {
st[s.charCodeAt(i + 1) - 97][s.charCodeAt(i) - 97] = true;
}
for (let i = 0; i < s.length - 1; ++i) {
if (st[s.charCodeAt(i) - 97][s.charCodeAt(i + 1) - 97]) {
return true;
}
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.