First Letter to Appear Twice (LeetCode 2351): Solution and Explanation
Read the string left to right while remembering which letters you have already met. The first letter that is already remembered when you reach it is the answer, because its second copy is the earliest second copy in the string. For "abccbaacz" the scan stops at index 3 and returns 'c'.
Intuition
The order that matters is the order of second copies, and a left-to-right scan reaches second copies in exactly that order.
So the first time the scan sees a letter it has seen before, no other letter can have repeated earlier, and the scan can stop.
There are only 26 letters, so the memory is a small fixed-size set, an array of 26 booleans, or a 26-bit mask.
Approach: Single scan with a seen set
- Create an empty set
seen(or 26 booleans). - For each letter
cofs, check whethercis already inseen. - If it is, return
cright away. - Otherwise add
ctoseenand move on.
Step-by-step example
s = "abccbaacz"
aat index 0 is new: seen = {a}.bat index 1 is new: seen = {a, b}.cat index 2 is new: seen = {a, b, c}.cat index 3 is already in seen, so it is the first letter to appear twice. Return'c'.- The later repeats of
b(index 4) anda(index 5) are never reached.
Code: Python, Java, C++
1class Solution:
2 def repeatedCharacter(self, s: str) -> str:
3 seen = set()
4 for c in s:
5 if c in seen:
6 return c
7 seen.add(c)
8 return ""1class Solution {
2 public char repeatedCharacter(String s) {
3 boolean[] seen = new boolean[26];
4 for (char c : s.toCharArray()) {
5 if (seen[c - 'a']) {
6 return c;
7 }
8 seen[c - 'a'] = true;
9 }
10 return ' ';
11 }
12}1class Solution {
2public:
3 char repeatedCharacter(string s) {
4 bool seen[26] = {false};
5 for (char c : s) {
6 if (seen[c - 'a']) {
7 return c;
8 }
9 seen[c - 'a'] = true;
10 }
11 return ' ';
12 }
13};Time and space complexity
O(n)
Each letter is checked and added at most once, and by the pigeonhole principle the scan stops within 27 letters.
O(1)
The set never holds more than 26 letters.
Other approaches
Bit mask (time O(n), space O(1))
Use an integer whose bit c - 'a' marks a seen letter. Test with mask >> k & 1 and set with mask |= 1 << k. Same scan, no allocation.
Second-occurrence index per letter (time O(n), space O(1))
Record the index of every letter's second copy in one pass, then return the letter with the smallest such index. Correct but does more work than stopping early.
Common mistakes
- Returning the letter whose first copy is earliest. In
"abba"the answer is'b', not'a'. - Counting all letters first and returning the first one with count 2 or more in string order, which picks by first copy instead of second copy.
- Forgetting to stop at the first repeat and returning a later one.
Frequently asked questions
Why can the loop stop at the first repeat?
The scan reaches positions in increasing order, so the first repeat it finds is the earliest second copy of any letter. Nothing later can beat it.
Is the space really constant?
Yes. The alphabet has 26 letters, so the set is bounded by 26 entries no matter how long the string is.
Can the function fall off the end of the loop?
Not for valid input, since the problem guarantees a repeated letter. The final return only keeps compilers happy.