String Compression Problem Statement

Implement a program that performs basic string compression. When a character is consecutively repeated more than once, replace the consecutive duplicates with the count of repetitions.

Example:

Input:
If a string has "x" repeated 5 times as consecutive characters, it should be replaced by "x5".
Output:
The result will be the compressed version of the string, only replacing when the character count > 1.

Constraints:

  • The consecutive count of any character will be ≤ 9.
  • 0 <= N <= 106 where 'N' is the length of the string.
  • Time Limit: 1 second
Input Format:
The input consists of a single string with no leading or trailing spaces.
Output Format:
The output should be the compressed string printed in a single line.
Note:
You do not need to handle any console output operations; just implement the function to return the compressed string.
AnswerBot
1d

Implement a program to compress a string by replacing consecutive duplicates with the count of repetitions.

  • Iterate through the string and keep track of consecutive characters and their counts.

  • Replace ...read more

Help your peers!
Add answer anonymously...
Newgen Software Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter