LeetCode Perform String Shifts (Python)

Posted by 小明MaxMing on April 26, 2020

题目

You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]:

  • direction can be 0 (for left shift) or 1 (for right shift).
  • amount is the amount by which string s is to be shifted.
  • A left shift by 1 means remove the first character of s and append it to the end.
  • Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.

Return the final string after all operations.

解题思路

代码

class Solution:
    def stringShift(self, s: str, shift: List[List[int]]) -> str:
        final_shift = 0
        for direction, amount in shift:
            if direction:
                final_shift += amount
            else:
                final_shift -= amount
        final_shift %= len(s)
        return s[-final_shift:] + s[:-final_shift]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅