Skip to content

Commit d6df4b0

Browse files
committed
added: string compression
1 parent 484e08a commit d6df4b0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# string compression | leetcode 443 | https://leetcode.com/problems/string-compression/
2+
# sliding window to keep track of a char's occurence
3+
4+
class Solution:
5+
def compress(self, chars: list[str]) -> int:
6+
ptrL, ptrR = 0, 0
7+
total = 0
8+
chars += " "
9+
10+
while ptrR < len(chars):
11+
if chars[ptrL] != chars[ptrR]:
12+
chars[total] = chars[ptrL]
13+
total += 1
14+
group = ptrR - ptrL
15+
if group > 1:
16+
for x in str(group):
17+
chars[total] = x
18+
total += 1
19+
ptrL = ptrR
20+
ptrR += 1
21+
22+
return total

0 commit comments

Comments
 (0)