This commit is contained in:
parent
d74f67ea6b
commit
ed8c5a3088
1 changed files with 29 additions and 0 deletions
29
medium/string_compression_iii.py
Normal file
29
medium/string_compression_iii.py
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
# https://leetcode.com/problems/string-compression-iii
|
||||||
|
|
||||||
|
class Solution:
|
||||||
|
def compressedString(self, word: str) -> str:
|
||||||
|
last_char = None
|
||||||
|
l = 0
|
||||||
|
result = []
|
||||||
|
def to_string(ch, cnt, result):
|
||||||
|
if cnt <= 9:
|
||||||
|
result.append(str(cnt))
|
||||||
|
result.append(ch)
|
||||||
|
else:
|
||||||
|
cnt -= 9
|
||||||
|
result.append('9')
|
||||||
|
result.append(ch)
|
||||||
|
to_string(ch, cnt, result)
|
||||||
|
for ch in word:
|
||||||
|
if last_char == None:
|
||||||
|
last_char = ch
|
||||||
|
l = 1
|
||||||
|
continue
|
||||||
|
if last_char != ch:
|
||||||
|
to_string(last_char, l, result)
|
||||||
|
l = 1
|
||||||
|
last_char = ch
|
||||||
|
else:
|
||||||
|
l += 1
|
||||||
|
to_string(last_char, l, result)
|
||||||
|
return ''.join(result)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue