A single badge reader guards the entrance to a secure vault. Visitors swipe one after another, and each visitor's badge is stamped with a single lowercase letter. The string s records the sequence of letters swiped, in the exact order the swipes happened.
You must find the letter that is the first one whose swipe count reaches two -- that is, scanning the string from left to right, report the letter at the earliest position that has already appeared earlier in the string. It is guaranteed that at least one letter is swiped more than once, so an answer always exists.
A single line containing the string s, consisting only of lowercase English letters.
Print the single lowercase letter that is the first to reach a second swipe.
2 <= s.length <= 1000s consists of lowercase English letters only.s.Example 1
Input
abccbaacz
Expected
c
Explanation
Swiping left to right: a (new), b (new), c (new), c -> this is the second swipe of 'c', which is earlier than any other letter's second swipe (b's second swipe and a's second swipe both come later). So the answer is 'c'.
Example 2
Input
abcdd
Expected
d
Explanation
Swiping left to right: a, b, c, d (all new), then d again -- the second swipe of 'd' is the first repeat encountered, so the answer is 'd'.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →