Dsa
Strings
Medium
String Compression
Implement a method to compress a string using counts of repeated characters. If the compressed string is not smaller than the original string, return the original string. For example, the string 'aaabbbccc' would become 'a3b3c3'.
Example:
Input: 'aabcccccaaa'
Output: 'a2b1c5a3'
Example:
Input: 'abc'
Output: 'abc' (not compressed as it is the same length)
Key concepts
stringscompressionlength
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now