class Solution: def compressString(self , test_string ): # write code here滑动窗口: left = right = 0 res = '' while right < len(test_string): if test_string[right] == test_string[left]: right +=1 else: res += test_string[left] + str(right - left) left = right res += test_string[left] + str(right - left) return res if len(res) < len(test_string) else test_string
aclass Solution: def compressString(self , test_string ): left, right = 0,0 strCount = [] while right < len(test_string): if test_string[right] == test_string[left]: right +=1 else: strCount.append([test_string[left], right-left]) left = right strCount.append([test_string[left], right - left]) print(strCount) res = "" if len(strCount)*2 >= len(test_string): res = test_string else: for c in strCount: res += c[0]+str(c[1]) print(res) return res