BackmediumStrings Razorpay

Galactic Transmission Encoding Solution

Problem Statement

In a deep space mission, transmissions are encoded for efficiency. Given a string of 97 to 6787 characters consisting of lowercase English letters representing signal patterns, write a function to compress it using a custom encoding where sequences of the same letters are replaced by the letter followed by the count of its consecutive occurrences, except when the count is 1, in which case only the letter is output, and sequences of exactly two identical letters are left uncompressed.

Example 1
Input
aabbbcccccc
Output
aab3c6

Explanation: The sequence 'aa' is left as is because it has exactly two characters, 'bbb' is compressed to '3b', and 'cccccc' is compressed to '6c'.

Example 2
Input
xyz
Output
xyz

Explanation: Each character appears only once, so no compression is applied.

Example 3
Input
aaaaaaaaaa
Output
a10

Explanation: The sequence of ten 'a's is compressed to 'a10'.

Constraints

  • The input string will only contain lowercase English letters.
  • The length of the input string will be between 97 and 6787 characters (inclusive).
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free