A noisy export has left runs of the same character stuck together and you need to squeeze each run down to a single character.

Write a method that walks through the text and keeps a character only when it is different from the character immediately before it. Every run of two or more identical characters collapses into one, and characters that are already alone are kept as they are.

The comparison is case sensitive, so a and A are two different characters and both survive. Spaces and punctuation are ordinary characters and squeeze just like letters. A null or empty input returns an empty string.

Examples:

Input: 'aaabbbccc' Output: 'abc' Explanation: Each run of three identical characters collapses to one Input: 'abc' Output: 'abc' Explanation: Nothing repeats so the text comes back unchanged Input: 'aAaa' Output: 'aAa' Explanation: Only the final run of two lowercase a characters collapses Input: 'hello world!!' Output: 'helo world!' Explanation: The double l, the run of spaces and the double ! each squeeze to one Input: null Output: '' Explanation: A null or empty input returns an empty string
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 December 2026

Contest runs December 1 - 31. Complete challenges to climb the leaderboard!

Prize 1: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In