Skip to content

Commit 480be5b

Browse files
committed
2020-03-15
1 parent 990ebdd commit 480be5b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def compressString(self, S):
3+
"""
4+
:type S: str
5+
:rtype: str
6+
"""
7+
pre = None
8+
cnt = 0
9+
res = ""
10+
for ch in S:
11+
if not pre:
12+
cnt += 1
13+
pre = ch
14+
else:
15+
if ch == pre:
16+
cnt += 1
17+
else:
18+
res += pre + str(cnt)
19+
cnt = 1
20+
pre = ch
21+
if S:
22+
res += pre + str(cnt)
23+
return res if len(res) < len(S) else S

0 commit comments

Comments
 (0)