First Letter to Appear Twice (LeetCode 2351): Solution and Explanation

Easy
Hash Table
String
Bit Manipulation
Counting

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

  1. Create an empty set seen (or 26 booleans).
  2. For each letter c of s, check whether c is already in seen.
  3. If it is, return c right away.
  4. Otherwise add c to seen and move on.

Step-by-step example

s = "abccbaacz"
  1. a at index 0 is new: seen = {a}.
  2. b at index 1 is new: seen = {a, b}.
  3. c at index 2 is new: seen = {a, b, c}.
  4. c at index 3 is already in seen, so it is the first letter to appear twice. Return 'c'.
  5. The later repeats of b (index 4) and a (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 ""
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each letter is checked and added at most once, and by the pigeonhole principle the scan stops within 27 letters.

Space

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.