BackmediumStringsUberRazorpay

Consecutive Character Encoder Solution

Problem Statement

Given a string s consisting of lowercase English letters, implement 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
aabcccccaaa
Output
a1b1c5a3a

Explanation: Step-by-step: 1. Initialize an empty string result and a counter count to 1. 2. Iterate through the input string s. 3. If the current character is the same as the previous one, increment the count. 4. If the current character is different from the previous one, append the previous character and count to the result and reset the count to 1. 5. If the count is greater than 1, append the current character and count to the result. 6. Return the result.

Example 2
Input
a1b1c1d1e1f1
Output
a1b1c1d1e1f1

Explanation: Step-by-step: 1. The input string is already in the desired format, so no compression is needed. 2. Return the input string as the result.

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 Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Consecutive Character Encoder — Problem Statement & Solution Guide

StringsMediumMixed
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string s consisting of lowercase English letters, implement 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.

Examples

Example 1

Input

aabcccccaaa

Output

a1b1c5a3a

Explanation: Step-by-step: 1. Initialize an empty string result and a counter count to 1. 2. Iterate through the input string s. 3. If the current character is the same as the previous one, increment the count. 4. If the current character is different from the previous one, append the previous character and count to the result and reset the count to 1. 5. If the count is greater than 1, append the current character and count to the result. 6. Return the result.

Example 2

Input

a1b1c1d1e1f1

Output

a1b1c1d1e1f1

Explanation: Step-by-step: 1. The input string is already in the desired format, so no compression is needed. 2. Return the input string as the result.

Constraints

  • The input string will only contain lowercase English letters.
  • The length of the input string will be between 97 and 6787 characters (inclusive).

Optimal Approach & Strategy

A more efficient solution involves iterating over the string once and using a counter to keep track of consecutive occurrences of each letter. This approach would have a linear time complexity.

Brute Force Approach

One naive approach is to use nested loops to compare each character with the next ones and build the compressed string. This would result in a time complexity of O(n²). Another brute-force method could involve dividing the string into substrings and checking for compressible sequences.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

UberRazorpay

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.