Skip to content
LC-3163 Medium LeetCode

3163. String Compression III

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 67% Topics: String
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

# string
class Solution(object):
    def compressedString(self, word):
        """
        :type word: str
        :rtype: str
        """
        result = []
        cnt = 0
        for i in xrange(len(word)):
            cnt += 1
            if cnt == 9 or (i+1 == len(word) or word[i+1] != word[i]):
                result.append("%s%s" % (cnt, word[i]))
                cnt = 0
        return "".join(result)

Solution from kamyu104/LeetCode-Solutions · MIT