Skip to content

Commit 29c7b39

Browse files
committed
Add solution for string compression
1 parent 635326e commit 29c7b39

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

array/string-compression/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 443. String Compression
2+
3+
## Description
4+
See https://leetcode.com/problems/string-compression/description/
5+
6+
## Problem
7+
Given an array of characters `chars`, compress it using the following algorithm:
8+
9+
Begin with an empty string `s`. For each group of consecutive repeating characters in `chars`:
10+
11+
If the group's length is `1`, append the character to `s`.
12+
Otherwise, append the character followed by the group's length.
13+
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 in chars.
14+
15+
After you are done modifying the input array, return the new length of the array.
16+
17+
You must write an algorithm that uses only constant extra space.
18+
19+
## Example 1
20+
21+
```
22+
Input: chars = ["a","a","b","b","c","c","c"]
23+
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
24+
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
25+
```
26+
27+
## Example 2
28+
29+
```
30+
Input: chars = ["a"]
31+
Output: Return 1, and the first character of the input array should be: ["a"]
32+
Explanation: The only group is "a", which remains uncompressed since it's a single character.
33+
```
34+
35+
## Example 3
36+
37+
```
38+
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
39+
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
40+
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
41+
```
42+
43+
## Constraints
44+
45+
```
46+
1 <= chars.length <= 2000
47+
chars[i] is a lowercase English letter, uppercase English letter, digit, or symbol.
48+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def compress(chars: list[str]) -> int:
2+
if len(chars) == 0:
3+
return 0
4+
read, write = 0, 0
5+
while read < len(chars):
6+
char = chars[read]
7+
count = 0
8+
while read < len(chars) and chars[read] == char:
9+
read += 1
10+
count += 1
11+
chars[write] = char
12+
write += 1
13+
14+
if count > 1:
15+
for digit in str(count):
16+
chars[write] = digit
17+
write += 1
18+
return write
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from stringcompression import compress
2+
3+
def test_example_1():
4+
chars = ["a","a","b","b","c","c","c"]
5+
assert compress(chars) == 6
6+
assert chars[:6] == ["a","2","b","2","c","3"]
7+
8+
def test_example_2():
9+
chars = ["a"]
10+
assert compress(chars) == 1
11+
assert chars[:1] == ["a"]
12+
13+
def test_example_3():
14+
chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
15+
assert compress(chars) == 4
16+
assert chars[:4] == ["a","b","1","2"]
17+
18+
def test_example_4():
19+
chars = ["a","a","a","b","b","a","a"]
20+
assert compress(chars) == 6
21+
assert chars[:6] == ["a","3","b","2","a","2"]
22+
23+
def test_example_5():
24+
chars = ["a","a","a","a","a","a","a","a","a","a"]
25+
assert compress(chars) == 3
26+
assert chars[:3] == ["a","1","0"]

0 commit comments

Comments
 (0)