Given a string s, remove every repeated character so that each character appears only once. Keep the first occurrence of each character and return the remaining characters in the order they first appear in s.
For example, the distinct characters of "programming" in order of first appearance are p, r, o, g, a, m, i, n, so the result is "progamin".
Function Signature
def remove_duplicates(s: str) -> str:
...
Rules
-
Each character is kept at the position of its first occurrence. Every later copy of it is dropped.
-
The kept characters stay in the relative order they have in
s
. The output is not sorted.
Constraints
-
1 <= len(s) <= 10^5
-
s
consists of lowercase English letters.
Examples
Input: s = "programming"
Output: "progamin"
The first occurrences in order are p, r, o, g, a, m, i, n. The second r, the second m and the last g are dropped.
Input: s = "abcabc"
Output: "abc"
After the first abc, every character is a repeat, so nothing else is added.
Input: s = "bbbab"
Output: "ba"
b first appears before a, so the output is "ba". Sorting the distinct letters would give "ab", which is wrong here.