String Compression

Given a list of characters `chars`, compress them in-place using the following algorithm: replace sequences of repeating characters with the character followed by the count of repeats. Return the length of the compressed array.

Input Format

chars = array of characters

Output Format

length of compressed array after compression

Constraints

Examples

Example 1:

Input:

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

Output:

6

Explanation:

Compress to ["a","2","b","2","c","3"], length 6.

Example 2:

Input:

chars = ["a"]

Output:

1

Explanation:

Single char remains ["a"], length 1.

Example 3:

Input:

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

Output:

4

Explanation:

Compress to ['a','1','b','12'], length 4.

Loading...
String Compression - Two Pointers DSA Problem