Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Remove adjacent duplicate characters

Collapse consecutive identical characters in a string, keeping only the first of each adjacent run.

Python practice15 minString PatternsIntermediateLast updated March 18, 2026

Problem statement

Write a function that takes a string and removes adjacent duplicate characters: whenever one or more identical characters appear consecutively, they should be collapsed into a single instance of that character. The relative order of non-adjacent characters must be preserved. The function should be case-sensitive (i.e., 'A' and 'a' are different).

Task

Practice linear scanning of strings and understanding run-length-like transformations.

Examples

Collapse runs of identical letters

Input

'aaabcc' -> remove_adjacent_duplicates('aaabcc')

Output

'abc'

Runs 'aaa' -> 'a', 'cc' -> 'c', so the result is 'abc'.

Input format

A single string passed to remove_adjacent_duplicates(s).

Output format

A string with adjacent duplicates removed.

Constraints

Must run in O(n) time and O(1) extra space (ignoring result storage). Handle empty strings and preserve case-sensitivity.

Samples

Sample 1

Input

aaabbc

Output

abc

aaa -> a, bb -> b, c -> c