796. Rotate String旋转字符串

[抄题]:

We are given two strings, A and B.

shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = ‘abcde‘, then it will be ‘bcdea‘ after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Example 1:
Input: A = ‘abcde‘, B = ‘cdeab‘
Output: true

Example 2:
Input: A = ‘abcde‘, B = ‘abced‘
Output: false

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要用kmp:其实有时候简单拼一下就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(1) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

public boolean rotateString(String A, String B) {
    return A.length() == B.length() && (A + A).contains(B);
}

原文地址:https://www.cnblogs.com/immiao0319/p/9457745.html

时间: 2024-11-05 22:38:40

796. Rotate String旋转字符串的相关文章

[LeetCode] Rotate String 旋转字符串

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)

题目如下: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters c

LeetCode 796. Rotate String

原题链接在这里:https://leetcode.com/problems/rotate-string/ 题目: We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' aft

796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

[LeetCode&Python] Problem 796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

[LC] 796. Rotate String

We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can becom

[CareerCup] 1.8 String Rotation 字符串的旋转

1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of si using only one call to isSubstring (e.g.,"waterbottle"is a rotation of "

【编程题目】左旋转字符串 ☆

26.左旋转字符串(字符串)题目:定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部.如把字符串 abcdef 左旋转 2 位得到字符串 cdefab.请实现字符串左旋转的函数.要求时间对长度为 n 的字符串操作的复杂度为 O(n),辅助内存为 O(1). 思路: 设字符串为 abcdefg 要左旋两个, $$表示操作范围,|表示要旋转的轴线,将操作范围分为两个部分.每次把范围小的部分与范围大的部分的靠近|的位置交换.范围相同则直接交换. 1. $ab|cdefg$ 2    

【程序员编程艺术】学习记录1:左旋转字符串之指针翻转法

[程序员编程艺术]学习记录1:左旋转字符串之指针翻转法 题目:左旋转字符串 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部,如把字符串abcdef左旋转2位得到字符串cdefab.请实现字符串左旋转的函数,要求对长度为n的字符串操作的时间复杂度为O(n),空间复杂度为O(n) 思路一.暴力移位法 //暴力移位法 void leftshiftone(char *s, int n) { char t = s[0]; for(int i = 1;i < n; i++) s[i-1]