Remove Repeated Letters in First-occurrence Order
Problem statement
Keep only the first occurrence of every letter in s, preserving the original order. Return the resulting string.
For example, MALAYALAM becomes MALY. Do not sort the letters or choose a different subsequence. As a follow-up, solve this without a dictionary or hash map.
Function
removeDuplicateLetters(s: String) → StringExamples
Example 1
s = "MALAYALAM"return = "MALY"The first M, A, L and Y occur at indices 0, 1, 2 and 4. Every later character has already appeared.
Example 2
s = "BANANA"return = "BAN"Keep B at index 0, A at index 1 and N at index 2; skip the remaining repeats.
Constraints
0 <= s.length <= 2000.- s contains only uppercase English letters A through Z.