Talentd
FleetCode

2351. First Letter to Appear Twice

Easy74.9% acceptance2 companies

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.

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 <= 100
  • s consists of lowercase English letters.
  • s has at least one repeated letter.

Asked at 2 companies

Hints open one at a time

Hint 1
Iterate through the string from left to right. Keep track of the elements you have already seen in a set.
Hint 2
If the current element is already in the set, return that element.
3:18
Still stuck?Read 4 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def repeatedCharacter(self, s: str) -> str:
3
Starter codePython 3Spaces: 4
Examples
Input
"abccbaacz"
Input
"abcdd"
Your code is saved on this device.Solution