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
4mo

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!
Select
Add answer anonymously...

Newgen Software Technologies Software Engineer interview questions & answers

A Software Engineer was asked 10mo agoQ. What are the basic concepts of OOPS?
A Software Engineer was asked 10mo agoQ. Create a C++ class and demonstrate its implementation.
A Software Engineer was asked Q. How do you concatenate two linked lists in an alternating manner?

Popular interview questions of Software Engineer

A Software Engineer was asked 10mo agoQ1. What are the basic concepts of OOPS?
A Software Engineer was asked Q2. How do you concatenate two linked lists in an alternating manner?
A Software Engineer was asked Q3. How do you compile all Java files inside a folder using the command line?
Newgen Software Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits