First Letter to Appear Twice
Given a string s consisting of lowercase English letters, return the first letter to appear twice.
Note:
- A letter
aappears twice before another letterbif the second occurrence ofais before the second occurrence ofb. swill contain at least one letter that appears twice.
Example 1
Input
s = "abccbaacz"Output
"c"The letter
c is the first letter to appear twice because the index of its second occurrence is the smallest among all repeated letters.Example 2
Input
s = "abcdd"Output
"d"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.