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, return true if t is an anagram of s, and false otherwise.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104s and t consist of lowercase English letters.Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Problem summary: Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"anagram" "nagaram"
"rat" "car"
group-anagrams)palindrome-permutation)find-all-anagrams-in-a-string)find-resultant-array-after-removing-anagrams)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #242: Valid Anagram
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] cnt = new int[26];
for (int i = 0; i < s.length(); ++i) {
++cnt[s.charAt(i) - 'a'];
--cnt[t.charAt(i) - 'a'];
}
for (int i = 0; i < 26; ++i) {
if (cnt[i] != 0) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #242: Valid Anagram
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
cnt := [26]int{}
for i := 0; i < len(s); i++ {
cnt[s[i]-'a']++
cnt[t[i]-'a']--
}
for _, v := range cnt {
if v != 0 {
return false
}
}
return true
}
# Accepted solution for LeetCode #242: Valid Anagram
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
cnt = Counter(s)
for c in t:
cnt[c] -= 1
if cnt[c] < 0:
return False
return True
// Accepted solution for LeetCode #242: Valid Anagram
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
let n = s.len();
let m = t.len();
if n != m {
return false;
}
let mut s = s.chars().collect::<Vec<char>>();
let mut t = t.chars().collect::<Vec<char>>();
s.sort();
t.sort();
for i in 0..n {
if s[i] != t[i] {
return false;
}
}
true
}
}
// Accepted solution for LeetCode #242: Valid Anagram
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}
const cnt = new Array(26).fill(0);
for (let i = 0; i < s.length; ++i) {
++cnt[s.charCodeAt(i) - 'a'.charCodeAt(0)];
--cnt[t.charCodeAt(i) - 'a'.charCodeAt(0)];
}
return cnt.every(x => x === 0);
}
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.