Problem Solving/LeetCode
2023.03.02 Today's Challenge
fabichoi
2023. 3. 2. 23:45
https://leetcode.com/problems/string-compression/
String Compression - LeetCode
Can you solve this real interview question? String Compression - Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: * If the group's leng
leetcode.com
문제 되게 이상하네 -_-
class Solution:
def compress(self, chars: List[str]) -> int:
ans = 0
i = 0
while i < len(chars):
letter = chars[i]
count = 0
while i < len(chars) and chars[i] == letter:
count += 1
i += 1
chars[ans] = letter
ans += 1
if count > 1:
for c in str(count):
chars[ans] = c
ans += 1
return ans
반응형