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 consisting of lowercase English letters, return the first letter to appear twice.
Note:
a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.s will contain at least one letter that appears twice.Example 1:
Input: s = "abccbaacz" Output: "c" Explanation: The letter 'a' appears on the indexes 0, 5 and 6. The letter 'b' appears on the indexes 1 and 4. The letter 'c' appears on the indexes 2, 3 and 7. The letter 'z' appears on the index 8. The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
Example 2:
Input: s = "abcdd" Output: "d" Explanation: The only letter that appears twice is 'd' so we return 'd'.
Constraints:
2 <= s.length <= 100s consists of lowercase English letters.s has at least one repeated letter.Problem summary: Given a string s consisting of lowercase English letters, return the first letter to appear twice. Note: A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b. s will contain at least one letter that appears twice.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Bit Manipulation
"abccbaacz"
"abcdd"
two-sum)first-unique-character-in-a-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2351: First Letter to Appear Twice
class Solution {
public char repeatedCharacter(String s) {
int[] cnt = new int[26];
for (int i = 0;; ++i) {
char c = s.charAt(i);
if (++cnt[c - 'a'] == 2) {
return c;
}
}
}
}
// Accepted solution for LeetCode #2351: First Letter to Appear Twice
func repeatedCharacter(s string) byte {
cnt := [26]int{}
for i := 0; ; i++ {
cnt[s[i]-'a']++
if cnt[s[i]-'a'] == 2 {
return s[i]
}
}
}
# Accepted solution for LeetCode #2351: First Letter to Appear Twice
class Solution:
def repeatedCharacter(self, s: str) -> str:
cnt = Counter()
for c in s:
cnt[c] += 1
if cnt[c] == 2:
return c
// Accepted solution for LeetCode #2351: First Letter to Appear Twice
impl Solution {
pub fn repeated_character(s: String) -> char {
let mut vis = [false; 26];
for &c in s.as_bytes() {
if vis[(c - b'a') as usize] {
return c as char;
}
vis[(c - b'a') as usize] = true;
}
' '
}
}
// Accepted solution for LeetCode #2351: First Letter to Appear Twice
function repeatedCharacter(s: string): string {
const vis = new Array(26).fill(false);
for (const c of s) {
const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
if (vis[i]) {
return c;
}
vis[i] = true;
}
return ' ';
}
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.