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 two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Explanation:
The strings s and t can be made identical by:
'e' to 'a'.'g' to 'd'.Example 2:
Input: s = "f11", t = "b23"
Output: false
Explanation:
The strings s and t can not be made identical as '1' needs to be mapped to both '2' and '3'.
Example 3:
Input: s = "paper", t = "title"
Output: true
Constraints:
1 <= s.length <= 5 * 104t.length == s.lengths and t consist of any valid ascii character.Problem summary: Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"egg" "add"
"foo" "bar"
"paper" "title"
word-pattern)find-and-replace-pattern)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #205: Isomorphic Strings
class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> d1 = new HashMap<>();
Map<Character, Character> d2 = new HashMap<>();
int n = s.length();
for (int i = 0; i < n; ++i) {
char a = s.charAt(i), b = t.charAt(i);
if (d1.containsKey(a) && d1.get(a) != b) {
return false;
}
if (d2.containsKey(b) && d2.get(b) != a) {
return false;
}
d1.put(a, b);
d2.put(b, a);
}
return true;
}
}
// Accepted solution for LeetCode #205: Isomorphic Strings
func isIsomorphic(s string, t string) bool {
d1 := [256]int{}
d2 := [256]int{}
for i := range s {
if d1[s[i]] != d2[t[i]] {
return false
}
d1[s[i]] = i + 1
d2[t[i]] = i + 1
}
return true
}
# Accepted solution for LeetCode #205: Isomorphic Strings
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
d1 = {}
d2 = {}
for a, b in zip(s, t):
if (a in d1 and d1[a] != b) or (b in d2 and d2[b] != a):
return False
d1[a] = b
d2[b] = a
return True
// Accepted solution for LeetCode #205: Isomorphic Strings
use std::collections::HashMap;
impl Solution {
fn help(s: &[u8], t: &[u8]) -> bool {
let mut map = HashMap::new();
for i in 0..s.len() {
if map.contains_key(&s[i]) {
if map.get(&s[i]).unwrap() != &t[i] {
return false;
}
} else {
map.insert(s[i], t[i]);
}
}
true
}
pub fn is_isomorphic(s: String, t: String) -> bool {
let (s, t) = (s.as_bytes(), t.as_bytes());
Self::help(s, t) && Self::help(t, s)
}
}
// Accepted solution for LeetCode #205: Isomorphic Strings
function isIsomorphic(s: string, t: string): boolean {
const d1: number[] = new Array(256).fill(0);
const d2: number[] = new Array(256).fill(0);
for (let i = 0; i < s.length; ++i) {
const a = s.charCodeAt(i);
const b = t.charCodeAt(i);
if (d1[a] !== d2[b]) {
return false;
}
d1[a] = i + 1;
d2[b] = i + 1;
}
return true;
}
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.