Skip to content
LC-0848 Medium LeetCode

848. Shifting Letters

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

class Solution(object):
    def shiftingLetters(self, S, shifts):
        """
        :type S: str
        :type shifts: List[int]
        :rtype: str
        """
        result = []
        times = sum(shifts) % 26
        for i, c in enumerate(S):
            index = ord(c) - ord('a')
            result.append(chr(ord('a') + (index+times) % 26))
            times = (times-shifts[i]) % 26
        return "".join(result)

Solution from kamyu104/LeetCode-Solutions · MIT