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. JENKINS针对不同项目组对用户进行权限分配

    权限需求 因JENKINS上存有de(开发).te(测试).re(预发布)等三个不同环境的项目,同时因为项目需求,需要对不同的开发及测试人员配置不同的jenkins权限,即以项目为单位,对不同人员进行 ...

  2. MySQL学习5 - 数据类型二.md

    一 字符类型 二 枚举类型和集合类型 一 字符类型 #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的 ...

  3. 【Java】Java随手记

    System.out.printf() : System.out.printf("%d",x);               输出整数 System.out.printf(&quo ...

  4. python 模块 DButils

    # DButils 为了解决多客户端都需要操作数据库的问题. # import pymysql # from DBUtils.PooledDB import PooledDB # # POOL = P ...

  5. linux tomcat单机部署多应用

    1.修改/etc/profile 增加tomcat环境变量

  6. 论文笔记:Deep feature learning with relative distance comparison for person re-identification

    这篇论文是要解决 person re-identification 的问题.所谓 person re-identification,指的是在不同的场景下识别同一个人(如下图所示).这里的难点是,由于不 ...

  7. Java消息队列--ActiveMq 初体验

    1.下载安装ActiveMQ ActiveMQ官网下载地址:http://activemq.apache.org/download.html ActiveMQ 提供了Windows 和Linux.Un ...

  8. Lnmp下pureftpd新建FTP账户权限不足解决方法

    解决办法:  登录服务器.执行以下命令 chattr -i /home/wwwroot/default/.user.ini chown www:www -R /home/wwwroot/你的lnmp安 ...

  9. Mysql --库和表的操作

    库的增删改查 系统数据库 创建数据库 数据库的相关操作 表的操作 存储引擎介绍(有点多 很啰唆) 表的介绍 表的操作 一.系统数据库 查看系统库: show databases; nformation ...

  10. 【原创】大数据基础之Parquet(1)简介

    http://parquet.apache.org 层次结构: file -> row groups -> column chunks -> pages(data/index/dic ...