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

Note:

  • A and B will have length at most 100.

这道题给了我们两个字符串A和B,定义了一种偏移操作,以某一个位置将字符串A分为两截,并将两段调换位置,如果此时跟字符串B相等了,就说明字符串A可以通过偏移得到B。现在就是让我们判断是否存在这种偏移,那么最简单最暴力的方法就是遍历所有能将A分为两截的位置,然后用取子串的方法将A断开,交换顺序,再去跟B比较,如果相等,返回true即可,遍历结束后,返回false,参见代码如下:

解法一:

class Solution {
public:
bool rotateString(string A, string B) {
if (A.size() != B.size()) return false;
for (int i = ; i < A.size(); ++i) {
if (A.substr(i, A.size() - i) + A.substr(, i) == B) return true;
}
return false;
}
};

还有一种一行完成碉堡了的方法,就是我们其实可以在A之后再加上一个A,这样如果新的字符串(A+A)中包含B的话,说明A一定能通过偏移得到B。就比如题目中的例子,A="abcde", B="bcdea",那么A+A="abcdeabcde",里面是包括B的,所以返回true即可,参见代码如下:

解法二:

class Solution {
public:
bool rotateString(string A, string B) {
return A.size() == B.size() && (A + A).find(B) != string::npos;
}
};

参考资料:

https://leetcode.com/problems/rotate-string/solution/

https://leetcode.com/problems/rotate-string/discuss/118696/C++-Java-Python-1-Line-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Rotate String 旋转字符串的更多相关文章

  1. Leetcode796.Rotate String旋转字符串

    给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结果就是'bcdea' .如果在若干次旋转操作之后,A 能变成B ...

  2. 796. Rotate String旋转字符串

    [抄题]: We are given two strings, A and B. A shift on A consists of taking string A and moving the lef ...

  3. [LeetCode] Rotate List 旋转链表

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  4. 【LeetCode】796. 旋转字符串

    796. 旋转字符串 知识点:字符串:KMP算法: 题目描述 给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结 ...

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

    题目例如以下: Given an input string, reverse the string word by word. For example, Given s = "the sky ...

  6. [LeetCode] Rotate Array 旋转数组

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

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

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

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

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

  9. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

随机推荐

  1. spring boot打包

    1.application继承SpringBootServletInitializer  重写configure方法如下图 2.去掉项目中多余的main方法 3.将pom.xml打包改为war包 4. ...

  2. Yii2.0连接多个数据库

    Yii2.0连接多个数据库    一个项目根据需要会要求连接多个数据库,这里记录下实际项目中的操作流程.包括对数据库连接的配置以及如何生成模型文件,在控制器中加以运用. 一.配置 打开数据库配置文件c ...

  3. smartgit

    1.同步最新分支 2.smartgit ctrl+2 可以看到本地新增加的文件

  4. LeetCode第十二题-将数字转化为罗马数字

    Integer to Roman 问题简介:将输入的int类型数字转化为罗马数字 问题详解:罗马数字由七个不同的符号表示:I,V,X,L,C,D和M 符号-数值 I - 1 V - 5 X -10 L ...

  5. SSH和SSM对比总结

    当下流行的两种企业开发MVC开源框架,是我们Java程序猿必备知识能力.MVC,即模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界 ...

  6. 使用Python正则表达式自己实现解析URL各参数

    不使用库,自己可以利用正则表达式实现解析域名各部分参数:协议.域名.端口.路径.载荷等. 使用的正则表达式如下: r'''(?x)\A ([a-z][a-z0-9+\-.]*):// # Scheme ...

  7. canvas图片与img图片的相互转换

    最近在一个项目中,遇到了一个问题,需要把生成的canvas形式的二维码转换为图片,可以长按识别,保存等.查找了一些资料归纳总结了一些知识. 默认在jq库里进行,引入jquery.qrcode.min. ...

  8. JavaScript入门学习笔记(二)

    JavaScript运算符: 算术运算符.赋值运算符和字符串连接运算符. 算法运算符与复制运算符用法参照Java: 字符串运算符: +运算符用于把文本值或字符串变量连接起来,适用于两个或更多字符串变量 ...

  9. Java_数据类型

    变量就是申请内存来存储值. java的两大数据类型:内置数据类型和引用数据类型 数据类型 6种数据类型(4种整数型,2种浮点型),一种字符类型,一种布尔类型 数据类型 位数 描述 byte 8位 -1 ...

  10. Git使用九:合并和删除分支

    实际开发中的分支 目前的快照情况:git log --decorate --all --oneline --graph 可以看到,目前HEAD是指向的master分支 合并分支:git merge 分 ...