easy +8 pts

Remove Duplicates from String

Strip consecutive duplicates from a string, keeping the original order.

Write a function `remove_duplicates(s: str) -> str` that takes a string `s` and returns a new string where every sequence of identical consecutive characters is collapsed to a single character. The relative order of characters must remain the same. For example, `'aabbcc'` becomes `'abc'`. If `s` is empty, return an empty string.

Constraints

Input length: 0 to 10,000 characters. Characters can be any Unicode string characters. The function should run in O(n) time and use O(n) extra space in the worst case.

Example

>>> remove_duplicates('aabbcc')
'abc'
>>> remove_duplicates('aaabbbccc')
'abc'
>>> remove_duplicates('hello')
'helo'
>>> remove_duplicates('')
''
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about comparing each character with the previous one.
Initialize an empty result list or string and append characters that differ from the last appended one.
Remember to handle the empty string case.
You can avoid checking index boundaries by processing the first character separately or using a sentinel.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.