We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 484e08a commit d6df4b0Copy full SHA for d6df4b0
14. Questions/leetcode 443 - string compression.py
@@ -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
19
+ ptrL = ptrR
20
+ ptrR += 1
21
22
+ return total
0 commit comments