bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 443. String Compression

443. String Compression

Leetcode Solutions

Key approach of the solution

  1. Initialize two pointers i and res to 0.
  2. While i is less than the length of chars: a. Set groupLength to 1. b. While the next character is the same as chars[i], increment groupLength and i. c. Write the character chars[i] to chars[res] and increment res. d. If groupLength is greater than 1, convert groupLength to a string and write each digit to chars[res] incrementing res for each digit written. e. Increment i to move to the next group of characters.
  3. Return res as the new length of the array.
UML Thumbnail

Alternative approach using extra space for counting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...