Given a string, remove consecutive duplicate characters so that no two adjacent characters are the same. Only adjacent duplicates are removed — non-adjacent duplicates remain in the string. Return null if the input is null, and return an empty string if the input is empty.
Example 1:
Input: input = "aabbbcdd"
Output: "abcd"
Explanation: a appears twice consecutively, b appears three times consecutively, d appears twice consecutively — all are collapsed to a single character
Example 2:
Input: input = "abcabc"
Output: "abcabc"
Explanation: No characters are adjacent duplicates, so the string is returned unchanged
Example 3:
Input: input = "aaaa"
Output: "a"
Explanation: All four a characters are consecutive, so the result is a single a
Example 4:
Input: input = null
Output: null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.