Watch 10 video solutions for First Letter to Appear Twice, a easy level problem involving Hash Table, String, Bit Manipulation. This walkthrough by Engineering Digest has 2,353 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |