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.
You are given two strings s and t.
String t is generated by random shuffling string s and then add one more letter at a random position.
Return the letter that was added to t.
Example 1:
Input: s = "abcd", t = "abcde" Output: "e" Explanation: 'e' is the letter that was added.
Example 2:
Input: s = "", t = "y" Output: "y"
Constraints:
0 <= s.length <= 1000t.length == s.length + 1s and t consist of lowercase English letters.Problem summary: You are given two strings s and t. String t is generated by random shuffling string s and then add one more letter at a random position. Return the letter that was added to t.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation
"abcd" "abcde"
"" "y"
single-number)permutation-difference-between-two-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #389: Find the Difference
class Solution {
public char findTheDifference(String s, String t) {
int[] cnt = new int[26];
for (int i = 0; i < s.length(); ++i) {
++cnt[s.charAt(i) - 'a'];
}
for (int i = 0;; ++i) {
if (--cnt[t.charAt(i) - 'a'] < 0) {
return t.charAt(i);
}
}
}
}
// Accepted solution for LeetCode #389: Find the Difference
func findTheDifference(s, t string) byte {
cnt := [26]int{}
for _, ch := range s {
cnt[ch-'a']++
}
for i := 0; ; i++ {
ch := t[i]
cnt[ch-'a']--
if cnt[ch-'a'] < 0 {
return ch
}
}
}
# Accepted solution for LeetCode #389: Find the Difference
class Solution:
def findTheDifference(self, s: str, t: str) -> str:
cnt = Counter(s)
for c in t:
cnt[c] -= 1
if cnt[c] < 0:
return c
// Accepted solution for LeetCode #389: Find the Difference
impl Solution {
pub fn find_the_difference(s: String, t: String) -> char {
let s = s.as_bytes();
let t = t.as_bytes();
let n = s.len();
let mut count = [0; 26];
for i in 0..n {
count[(s[i] - b'a') as usize] += 1;
count[(t[i] - b'a') as usize] -= 1;
}
count[(t[n] - b'a') as usize] -= 1;
char::from(b'a' + (count.iter().position(|&v| v != 0).unwrap() as u8))
}
}
// Accepted solution for LeetCode #389: Find the Difference
function findTheDifference(s: string, t: string): string {
const cnt: number[] = Array(26).fill(0);
for (const c of s) {
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
for (const c of t) {
--cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
}
for (let i = 0; ; ++i) {
if (cnt[i] < 0) {
return String.fromCharCode(i + 'a'.charCodeAt(0));
}
}
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.