<AI/>
enru
All posts

String compression

LeetCode 443: Compress an array of characters in place by replacing repeated runs with the character and its count.

Difficulty
Medium
Published
Topics
#string#two pointers
Open on LeetCode

Problem Description

You are given a string as an array of characters. Write a function that compresses the input array as follows:

  • If a character repeats more than once in a row, replace the whole run with ['a', 'n'], where a is the original character and n is the number of consecutive repetitions. If n has multiple digits, each digit must be added as a separate character.
  • If a character doesn't repeat, leave it unchanged.

All changes must be made in place, and the function must return the number of characters in the compressed string.

Constraints

  • The input array has from 1 to 2000 characters
  • The elements are English letters, digits or symbols

Examples

Input

chars = ["a","a","b","b","c","c","c"]

Answer: 6

The input array must be transformed into ["a","2","b","2","c","3"]

Solution

The problem is quite straightforward; the main catch is replacing the elements in place.

We'll need several indices:

  • The index of the current element lastElemIdx
  • The index of the start of the run of equal elements firstElemIdx
  • The index of the element to be overwritten while compressing newPositionIdx. For efficiency, we overwrite elements of the input array and then "cut off" the tail. Otherwise, we would have to remove repeated characters by shifting the tail of the array n elements to the left, which would slow the algorithm down significantly.

We iterate over the input array:

  • While the current character equals the previous one, we move forward, remembering the last index of the repeated character
  • If the current element differs from the previous one (or we've reached the end of the array):
    1. Write the remembered character into the array at position newPositionIdx
    2. Increment newPositionIdx
    3. If needed, convert the difference between lastElemIdx and firstElemIdx into a string and write it character by character into the following cells of the array (incrementing newPositionIdx for each inserted character)
    4. Remember the new character as the start of the next run and move firstElemIdx

At the end, we just need to "cut off" the tail of the input array.

typescript
export const compress = (chars: string[]): number => {
  let newPositionIdx = 0;
 
  for (let firstElemIdx = 0; firstElemIdx < chars.length; firstElemIdx++) {
    const lastChar = chars[firstElemIdx];
 
    let lastElemIdx = firstElemIdx + 1;
    while (lastElemIdx < chars.length && chars[lastElemIdx] === lastChar) {
      lastElemIdx++;
    }
 
    chars[newPositionIdx] = chars[lastElemIdx - 1];
    newPositionIdx++;
 
    const counter = lastElemIdx - firstElemIdx;
    if (counter > 1) {
      for (const countChar of counter.toString()) {
        chars[newPositionIdx] = countChar;
        newPositionIdx++;
      }
    }
 
    firstElemIdx = lastElemIdx - 1;
  }
 
  chars.splice(newPositionIdx, chars.length - newPositionIdx);
  return chars.length;
};

Complexity Analysis

n is the number of elements in the array.

Time Complexity

O(n), since we iterate over all elements of the array.

Space Complexity

O(1), since we modify the array in place.