array and hashmap December 20, 2022

Shifting letters

Time O(n) Space O(1) Open original problem

We will calculate the total shift and then shift each character by the total shift. We will keep track of the total shift by subtracting the shift from the total shift. We will use a helper function to shift the character by the shift.

class Solution:
    def shiftingLetters(self, s: str, shifts: List[int]) -> str:
        totalShift = sum(shifts)
        res = []
        for i, ch in enumerate(s):
            res.append(self.shift(ch, totalShift))
            totalShift -= shifts[i]
        return ''.join(res)

    def shift(self, ch: str, shift: int) -> str:
        return chr((ord(ch) - ord('a') + shift) % 26 + ord('a'))

Time complexity: O(n)
Space complexity: O(1)