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 Overview: You receive a lowercase string s. Exactly one character appears twice before any other repeated character. The task is to return the first letter whose second occurrence appears earliest while scanning from left to right.
The constraint that the string only contains lowercase English letters makes the problem straightforward. You only need to detect the first repeated character while iterating through the string once. Efficient solutions rely on constant‑time membership checks using structures like a hash table or a fixed-size counting array.
Approach 1: Using a Set for Tracking Seen Characters (O(n) time, O(1) space)
Scan the string from left to right and maintain a set (or hash set) of characters that have already appeared. For each character, check whether it exists in the set. If it does, that means this is the second occurrence and it must be the earliest repeated character because the string is processed sequentially. Return that character immediately. If it is not in the set, insert it and continue scanning.
The key insight is that a hash lookup runs in constant time. Each character is processed exactly once, making the algorithm linear. Since the alphabet size is fixed (26 lowercase letters), the set never grows beyond 26 elements, so the space usage is effectively constant. This approach is simple, readable, and commonly expected in interviews when solving problems involving repeated elements in a string.
Approach 2: Using an Array for Counting Occurrences (O(n) time, O(1) space)
Instead of a hash-based structure, use a fixed integer array of size 26 to track character frequencies. Convert each character to an index using index = ch - 'a'. While iterating through the string, increment the corresponding counter. The moment a counter becomes 2, you have found the first letter whose second appearance occurs earliest, so return it immediately.
This approach works because the input domain is limited to lowercase English letters. Array indexing is faster and more memory‑predictable than a hash structure. Many low-level implementations in C or Java prefer this technique because it avoids hashing overhead and uses direct indexing, a common pattern in counting-based problems.
Recommended for interviews: The set-based approach is usually the first solution developers write because it directly models the idea of tracking previously seen characters. It demonstrates understanding of hash-based lookup and produces clean code. The counting-array version shows awareness of constraints and optimization opportunities. Interviewers typically accept either solution since both run in O(n) time with O(1) space, but explaining why the fixed-size array works can show stronger problem-solving awareness.
This approach involves iterating through the string and using a set data structure to keep track of characters that have already been seen. As soon as a character that has been seen before is found, we return it.
The function initializes an empty set called seen. As we iterate through the string s, we check if the character is already in the set. If it is, we return that character as it is the first to appear twice. Otherwise, we add the character to the set.
Python
JavaScript
Time Complexity: O(n) because each operation of adding to or checking membership of a set is O(1).
Space Complexity: O(1) as the space used by the set is limited to a constant size (26 letters)
This approach counts occurrences of each character using a fixed-size array where the index corresponds to the character's position in the alphabet. Once a character count exceeds 1, it is returned.
An array of 26 integers representing each lowercase letter is initialized to zero. As we iterate through the string, we increase the count corresponding to the character's index in this array. When a count reaches 2, we return the character.
Time Complexity: O(n), where n is the length of the string.
Space Complexity: O(1), since the array size is constant at 26.
We traverse the string s, using an array or hash table cnt to record the occurrence of each letter. When a letter appears twice, we return that letter.
The time complexity is O(n) and the space complexity is O(C). Here, n is the length of the string s, and C is the size of the character set. In this problem, C = 26.
We can also use an integer mask to record whether each letter has appeared, where the i-th bit of mask indicates whether the i-th letter has appeared. When a letter appears twice, we return that letter.
The time complexity is O(n) and the space complexity is O(1). Here, n is the length of the string s.
| Approach | Complexity |
|---|---|
| Using a Set for Tracking Seen Characters | Time Complexity: O(n) because each operation of adding to or checking membership of a set is O(1). |
| Using an Array for Counting Occurrences | Time Complexity: O(n), where n is the length of the string. |
| Array or Hash Table | — |
| Bit Manipulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Set for tracking seen characters | O(n) | O(1) | General solution in Python/JavaScript; clean and easy to reason about with constant-time hash lookups |
| Array for counting occurrences | O(n) | O(1) | Best when characters are limited to lowercase letters; avoids hashing and uses direct indexing |
LeetCode 2351: First Letter to Appear Twice Solution • Engineering Digest • 2,353 views views
Watch 9 more video solutions →Practice First Letter to Appear Twice with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor