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. python的C扩展调用,使用原生的python-C-Api

    1.在文件第一行包含python调用扩展的头文件 #include <Python.h> 2.用原生C写好需要调用的函数 int add_one(int a){ ; } 3.用python ...

  2. 神器cut基因剪

    cut cut 不就是切嘛,没错就是它--我给他起了一个外号基因剪刀 来我们学一下怎么使用这个命令 cut --help [root@ESProbe ~]# cut --help Usage: cut ...

  3. Spring扩展:替换IOC容器中的Bean组件 -- @Replace注解

    1.背景:     工作中是否有这样的场景?一个软件系统会同时有多个不同版本部署,比如我现在做的IM系统,同时又作为公司的技术输出给其他银行,不同的银行有自己的业务实现(比如登陆验证.用户信息查询等) ...

  4. spring的ioc依赖注入的三种方法(xml方式)

    常见的依赖注入方法有三种:构造函数注入.set方法注入.使用P名称空间注入数据.另外说明下注入集合属性 先来说下最常用的那个注入方法吧. 一.set方法注入 顾名思义,就是在类中提供需要注入成员的 s ...

  5. 物联网 软硬件系统 树莓派 单片机 esp32 小程序 网页 开发 欢迎相互交流学习~

    物联网软硬件开发 知识分享 多年学生项目开发经验 物联网 软硬件系统 树莓派 单片机 esp32 小程序 网页 开发 欢迎相互交流学习~ http://39.105.218.125:9000/

  6. thinkphp5.0.*命令执行批量脚本

    import requests import Queue import threading import time user_agent = "Mozilla/5.0 (Windows NT ...

  7. JSP+Servlet+C3P0+Mysql实现的网上蛋糕店

    本文存在视频版本,请知悉 项目简介 项目来源于:https://gitee.com/PositiveMumu/CakesShop/tree/master 这次分享一个蛋糕商场系统,还是很简单的系统.界 ...

  8. HDU-4252 A Famous City(单调栈)

    最后更新于2019.1.23 A Famous City ?戳这里可以前往原题 Problem Description After Mr. B arrived in Warsaw, he was sh ...

  9. sql-lib闯关11-20关

    从第11关开始,我们就进入到了POST注入的世界了. POSTpost是一种数据提交方式,它主要是指数据从客户端提交到服务器端,例如,我们常常使用的用户登录模块.网站的留言板模块等,在这些功能模块中我 ...

  10. 机器学习之强化学习概览(Machine Learning for Humans: Reinforcement Learning)

    声明:本文翻译自Vishal Maini在Medium平台上发布的<Machine Learning for Humans>的教程的<Part 5: Reinforcement Le ...