Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Remove Consecutive Duplicates

Easy Acceptance 60.00% Points 20.00

You are given a string s. Your task is to remove all consecutive duplicate characters. If the same characters appear next to each other, keep only one occurrence. Return the final string after removing duplicates.

Examples
Example 1

image Input: s = "aabbccdaa"

Output: "abcda"

Explanation: Consecutive duplicate characters are removed.

Example 2

image

Input: s = "aaaa"

Output: "a"

Explanation: All duplicates are consecutive, so only one character remains.

Example 3

image

Input: s = "abca"

Output: "abca"

Explanation: No consecutive duplicates are present, so the string remains unchanged.

Hints
Hint 1
N/A
Constraints
  • 1 <= length of s <= 10^5
  • The string contains lowercase or uppercase letters only
Companies
Apple Cognizant
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions