Perform String Shifts

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.

Example 1:

Input: s = "abc", shift = [[0,1],[1,2]]

Output: "cab"

Explanation:

[0,1] means shift to left by 1. "abc" -> "bca"

[1,2] means shift to right by 2. "bca" -> "cab"

Example 2:

Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]

Output: "efgabcd"

Explanation:

[1,1] means shift to right by 1. "abcdefg" -> "gabcdef"

[1,1] means shift to right by 1. "gabcdef" -> "fgabcde"

[0,2] means shift to left by 2. "fgabcde" -> "abcdefg"

[1,3] means shift to right by 3. "abcdefg" -> "efgabcd"

Constraints:

1 <= s.length <= 100

s only contains lower case English letters.

1 <= shift.length <= 100

shift[i].length == 2

0 <= shift[i][0] <= 1

0 <= shift[i][1] <= 100

solution

这就是一个循环左移和右移的问题,循环左移时,把最左边一位拿出来,放在末尾即可,循环右移时把最后一位拿出来,放在开头即可

class Solution:
def stringShift(self, s: str, shift: List[List[int]]) -> str:
lst=list(s)
for i in range(len(shift)):
for j in range(shift[i][1]):
if shift[i][0] == 1:
tmp=lst[len(lst)-1]
lst.pop()
lst.insert(0,tmp)
else:
tmp=lst[0]
lst.remove(lst[0])
lst.append(tmp)
return "".join(lst)

分析:

时间复杂度:O(N);N为移动的次数

空间复杂度:O(1)

参考链接

leetcode

leetcode Perform String Shifts的更多相关文章

  1. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

  2. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  3. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  4. [LeetCode] Encode String with Shortest Length 最短长度编码字符串

    Given a non-empty string, encode the string such that its encoded length is the shortest. The encodi ...

  5. [LeetCode] Decode String 解码字符串

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  6. [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  7. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  8. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  9. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

随机推荐

  1. Html的总结(待完善)

    Html的总结(待完善) 框内文字 Placeholder 框内文字(例如:请输入密码) A标签 link 未点击的A标记 visited 点击过的A标签 hover 放置鼠标变颜色 active 点 ...

  2. java web 获取 网页访问次数

    ServletContext context = request.getServletContext(); /** * 从ServletContext中获取计数器对象 */Integer count ...

  3. [剑指offer]10.斐波那契数列+青蛙跳台阶问题

    10- I. 斐波那契数列 方法一 Top-down 用递归实现 def fibonacci(n): if n <= 0: return 0 if n == 1: return 1 return ...

  4. IdentityServer4源码解析_4_令牌发放接口

    目录 identityserver4源码解析_1_项目结构 identityserver4源码解析_2_元数据接口 identityserver4源码解析_3_认证接口 identityserver4 ...

  5. Qt 的信号与槽(纯干货)

    接触Qt断断续续有些时间了,总看了一堆的文章说信号槽的概念,心里就想骂人,做为一个初学者,最重要的就是怎么写代码,写代码写多了,再去看理论,有时水到渠成的就明白那些理论了.但所有讲信号槽的都把一堆信号 ...

  6. linux pdftk

    部分内容来源网络,如有版权问题,请联系删除: http://xuqin.blog.51cto.com/5183168/1117780,   http://blog.sina.com.cn/s/blog ...

  7. 使用Python创建自己的Instagram滤镜

    不知道你有没有使用过Instagram滤镜,它们非常方便,只需单击几个按钮,就可以变换我要发布的照片​​. 你是否想过自己可以创建一个?答案是可以的! 在本文中,我将向你展示如何使用代码和示例图像来创 ...

  8. 从DeepNet到HRNet,这有一份深度学习“人体姿势估计”全指南

    从DeepNet到HRNet,这有一份深度学习"人体姿势估计"全指南 几十年来,人体姿态估计(Human Pose estimation)在计算机视觉界备受关注.它是理解图像和视频 ...

  9. Jmeter4.0接口测试之WebServices(四)

    关于什么是web services,可以到W3C中查看详细的信息,本文章主要介绍使用Jmeter怎么来做web services的接口测试,首先它也是基于HTTP协议的,我们实现电话号码归属地的查询, ...

  10. python——体育竞技

    一.体育竞技分析基本规则两个球员,交替用球拍击球发球权,回合未能进行一次击打回合结束首先达到15分赢得比赛 1.自顶向下的设计 #7_game_2.py from random import * de ...