Remove Duplicate Letters from a String
Problem Descriptionβ
Write a function that accepts a string as its input and returns the same string with duplicate letters removed.
Tip: If you can solve this challenge without a for-in
loop, you can consider it βtrickyβ rather than βeasyβ.
Inputβ
- A single string.
Outputβ
- A string with duplicate letters removed, keeping only the first occurrence of each letter.
Constraintsβ
- The function should preserve the order of the first occurrences of the letters.
Exampleβ
Input:
"wombat"Output:
"wombat"Explanation:
The string "wombat" has no duplicate letters, so it remains unchanged.
Input:
"hello"Output:
"helo"Explanation:
The string "hello" has duplicate 'l' characters, so the second 'l' is removed.
Input:
"Mississippi"Output:
"Misp"Explanation:
The string "Mississippi" has multiple duplicate characters. After removing duplicates, the resulting string is "Misp".