Skip to content
LC-0880 Medium LeetCode

880. Decoded String at Index

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 37% Topics: String, Stack
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

class Solution(object):
    def decodeAtIndex(self, S, K):
        """
        :type S: str
        :type K: int
        :rtype: str
        """
        i = 0
        for c in S:
            if c.isdigit():
                i *= int(c)
            else:
                i += 1

        for c in reversed(S):
            K %= i
            if K == 0 and c.isalpha():
                return c

            if c.isdigit():
                i /= int(c)
            else:
                i -= 1

Solution from kamyu104/LeetCode-Solutions · MIT