Advertisement

String Compression - LeetCode 443 Solution

String Compression - Complete Solution Guide

String Compression is LeetCode problem 443, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given an array of characters chars , compress it using the following algorithm: Begin with an empty string s . For each group of consecutive repeating characters in chars : If the group's length is 1 , append the character to s . Otherwise, append the character followed by the group's length. The compressed string s should not be returned separately , but instead, be stored in the input character array chars . Note that group lengths that are 10 or longer will be split into multiple characters i

Detailed Explanation

The problem requires compressing a given array of characters `chars` in-place. Compression is achieved by replacing consecutive repeating characters with the character followed by the count of repetitions. If a character appears only once, it remains as is. The counts, if greater than 1, are appended to the array as individual characters. For example, 'aabbbccc' becomes 'a2b3c3'. The function should modify the input array directly and return the new length of the compressed array.

Solution Approach

The solution uses a two-pointer approach. `read_ptr` iterates through the input `chars` array, and `write_ptr` writes the compressed output to the same array, starting from index 0. The algorithm maintains a `count` of consecutive repeating characters. When a different character is encountered or the end of the array is reached, the current character and its count (if greater than 1) are written to the `chars` array using the `write_ptr`.

Step-by-Step Algorithm

  1. Step 1: Initialize `read_ptr` and `write_ptr` to 0, and `n` to the length of `chars`.
  2. Step 2: Iterate through the `chars` array using the `read_ptr`.
  3. Step 3: For each character, count the number of consecutive repeating characters using an inner loop.
  4. Step 4: Write the current character to the `chars` array at the `write_ptr` index, and increment `write_ptr`.
  5. Step 5: If the count of repeating characters is greater than 1, convert the count to a string. Iterate through this string, writing each digit character to the `chars` array at the `write_ptr` index, and increment `write_ptr` after each write.
  6. Step 6: Move the `read_ptr` to the next distinct character. Repeat steps 3-5 until `read_ptr` reaches the end of the array.
  7. Step 7: Return the `write_ptr`, which represents the new length of the compressed array.

Key Insights

  • Insight 1: The core idea is to use two pointers: one to read the input array and another to write the compressed characters to the same array.
  • Insight 2: We need to iterate through the array, counting consecutive repeating characters. Once the repeating sequence ends, we write the character and its count (if greater than 1) to the array.
  • Insight 3: Numbers greater than 9 need to be split into individual digits and written as separate characters into the compressed array. This is a critical implementation detail.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Two Pointers, String.

Companies

Asked at: Affirm, CrowdStrike, EPAM Systems, Expedia, Goldman Sachs, IBM, Lyft, Palo Alto Networks, Paytm, PhonePe, Pinterest, Ripple, Rivian, Salesforce, ServiceNow, Snap, Yandex, Yelp, Zoox.