...

/

Solution: String Compression

Solution: String Compression

Let’s solve the String Compression problem using the Two Pointers pattern.

Statement

Given an array of characters, chars, compress it in place according to the following rules:

  1. Start with an empty string s.

  2. For each group of consecutive repeating characters in chars:

    1. If the group length is 11, append just the character to s.

    2. Otherwise, append the character followed by the group length.

The compressed string s should not be returned separately; instead, it must be written directly into the input character array chars. Note that if a group’s length is 1010 ...

Ask