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. Silverlight/WPF 系列汇总

    Silverlight 解谜游戏系列 -- Silverlight 3 · Silverlight 解谜游戏 之一 新建项目 · Silverlight 解谜游戏 之二 创建题板 · Silverli ...

  2. 如何使用AB PLC仿真软件Studio 5000 Logix Emulate

    前言:在学习PLC编程或程序开发过程中,如果身边没有实体PLC,又想验证程序逻辑,这时,仿真软件是不错的选择.针对AB PLC的仿真软件Studio 5000 Logix Emulate,有的同学说: ...

  3. IN-子查询

    为什么需要子查询? 现实中,很多情况需要进行以下条件的判断 集合成员资格 某一元素是否是某一个集合的成员 集合之间的比较 某一个集合是否包含另一个集合 集合基数的测试 测试集合是否为空 测试集合是否存 ...

  4. 阿里的fastJson.jar jsonArray 和 list 互转

    阿里的fastJson.jar: //list转换为json List<CustPhone> list = new ArrayList<CustPhone>(); String ...

  5. 清北学堂学习总结day1

    上午篇 一.高精度计算: [以下内容先只考虑非负数情况] •高精度加法: 思路:[模拟竖式运算] 注意:[进位] •高精度减法: 思路:[同加法类似,模拟竖式运算,进位变退位] 注意: [结果为负数的 ...

  6. MVC ModelState.AddModelError使用

    cshtml:红色部分为使用方法  PostNextDay是 字段 <div id="postNextDay" class="col-xs-12 col-sm-6 ...

  7. make: 警告:检测到时钟错误。您的创建可能是不完整的。

    问题: make: 警告:检测到时钟错误.您的创建可能是不完整的. 原因:1. 文件时间不一致.(或者修改了系统时间) 如何解决:你touch *一下,然后重新编译 touch * touch命令有两 ...

  8. mysql收集统计信息

    一.手动  执行Analyze table innodb和myisam存储引擎都可以通过执行“Analyze table tablename”来收集表的统计信息,除非执行计划不准确,否则不要轻易执行该 ...

  9. [转]Ubuntu中apt与apt-get命令的区别

    转载于https://www.sysgeek.cn/apt-vs-apt-get/ Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入.其实早在 2014 年,apt 命令 ...

  10. Mac OSX 系统搭建 Java 开发环境

    转载:https://www.cnblogs.com/zjx2711904647/p/7735556.html 1. 安装JDK 双击jdk-9.0.1_osx-x64_bin.dmg文件进行安装 2 ...