We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ecd747b commit e01ed78Copy full SHA for e01ed78
Hacktober2023/StringCompression.py
@@ -0,0 +1,27 @@
1
+# Time: O(n)
2
+# Space: O(1)
3
+
4
+class Solution(object):
5
+ def compress(self, chars):
6
+ """
7
+ :type chars: List[str]
8
+ :rtype: int
9
10
+ anchor, write = 0, 0
11
+ for read, c in enumerate(chars):
12
+ if read+1 == len(chars) or chars[read+1] != c:
13
+ chars[write] = chars[anchor]
14
+ write += 1
15
+ if read > anchor:
16
+ n, left = read-anchor+1, write
17
+ while n > 0:
18
+ chars[write] = chr(n%10+ord('0'))
19
20
+ n /= 10
21
+ right = write-1
22
+ while left < right:
23
+ chars[left], chars[right] = chars[right], chars[left]
24
+ left += 1
25
+ right -= 1
26
+ anchor = read+1
27
+ return write
0 commit comments