Skip to content
RED SAGSkills
easy15 ptsstrings

Compress Character Runs

Compress a string by replacing every maximal run of the same character with the character followed by the run length. A run of length 1 is written as just the character.

For example "aaabcc" becomes "a3bc2". Characters are case-sensitive.

Example 1

Input: s = "aaabcc"
Output: "a3bc2"

Example 2

Input: s = "abc"
Output: "abc"

Constraints

  • 0 <= s.length <= 10^5
  • s contains letters only.
Hint 1

Walk the string and find where each run ends.

Hint 2

Build the result in an array and join once at the end to stay linear.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer, Embedded Systems Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.