Skip to main content

String Compression

MIDDLE

String Compression

Compress a string by replacing consecutive repeated characters with character + count.

Requirements:

  • Replace consecutive characters with char + count
  • If compressed is not shorter, return original
  • Only compress if count > 1

Example:

Input: "aabcccccaaa"
Output: "a2b1c5a3"

Input: "abc"
Output: "abc"

Examples:

Input 1:{"s":"aabcccccaaa"}
Output 1:"a2b1c5a3"
Input 2:{"s":"abc"}
Output 2:"abc"

Loading editor...

Run your code to see results

Click the Run button above