String Compression Task

Develop an algorithm that compresses a string by replacing consecutive duplicate characters with the character followed by the count of its repetitions, if that count exceeds 1.

Example:

Input:
"xxxxx"
Output:
"x5"
Explanation:

The character 'x' is repeated 5 times consecutively, thus it is represented as 'x5'. The string is only modified if a character appears more than once consecutively.

Constraints:

  • The consecutive occurrence count for each character in the string does not exceed 9.
  • 0 <= N <= 106, where 'N' represents the length of the input string.
Note:

You do not need to handle the output directly; focus on implementing the function to perform the task as specified.

AnswerBot
4d

Develop an algorithm to compress a string by replacing consecutive duplicate characters with the character followed by the count of its repetitions.

  • Iterate through the input string while keeping track...read more

Help your peers!
Add answer anonymously...
UST 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