Given a string s of lowercase English letters, return the first character in reading order that appears more than once anywhere in s. Scan s from left to right and return the first character whose total count in s is at least 2. If no character repeats, return an empty string "".
This is different from returning the character whose second occurrence comes earliest. For s = "abccba" this problem returns "a", because 'a' is the first character read and it occurs again later in the string. A "first letter to appear twice" approach would return "c", because the second 'c' is the earliest second occurrence.
Function Signature
def first_repeating_character(s: str) -> str:
Rules
-
Return the answer as a one-character string.
-
Among all characters whose total count in
s
is at least 2, return the one whose first occurrence is leftmost.
-
If every character appears exactly once, return
""
.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters.
Examples
Input: s = "abccba"
Output: "a"
'a' is the first character read, and it appears twice in total, so the answer is "a". The second 'c' is the earliest second occurrence, but that does not matter here.
Input: s = "adgfdcbbgf"
Output: "d"
'a' appears only once. 'd' is the next character and appears twice, so the answer is "d".
Input: s = "abcde"
Output: ""
Every letter appears exactly once, so no character repeats and the result is the empty string.