First Letter to Appear Twice

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
Inputs = "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
Inputs = "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.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate