easy +10 pts

Remove Consecutive Duplicate Letters

Compress strings by removing adjacent repeated characters while keeping order.

Write a function `remove_consecutive_duplicates(s: str) -> str` that takes a string `s` and returns a new string where any character that appears consecutively more than once is reduced to a single occurrence. Only consecutive duplicates are removed; non-adjacent occurrences of the same character remain unchanged. The function must preserve the original order of characters. If `s` is empty, return an empty string.

Constraints

Input string `s` can contain any printable ASCII characters (including spaces, digits, punctuation) and may be of length 0 to 10^5. The function should run in O(n) time and O(n) space.

Example

>>> remove_consecutive_duplicates("aabbcc")
'abc'
>>> remove_consecutive_duplicates("hello")
'helo'
>>> remove_consecutive_duplicates("")
''
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about comparing each character with the last kept character.
You can build the result string one character at a time.
A single pass through the string is sufficient; no need for lookahead.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.