DIY: String Compression

Solve the interview question "String Compression" in this lesson.

Problem statement

You are given a list of characters. Your task will be to compress this list, using the following algorithm:

For each group of consecutive repeating characters in the string:

  1. If the group’s length is 1, you will append the character to s.
  2. Otherwise, you will append the character followed by the group’s length.

Note: Groups of length 10 or longer will be split into multiple characters.

The space complexity of the algorithm should be constant. To attain that constancy, the compressed list should not be returned separately but, instead, should be stored in the input list.

After you are done modifying the input array, you will return the new length of the array.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.