leetcode-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 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:
AandBwill have length at most100.
要完成的函数:
bool rotateString(string A, string B)
说明:
1、给定两个字符串A和B,要求判断这两个字符串,可不可以通过不断地把A中的首字母搬到末尾去,最终把A变成B。比如A为abcde,B为cdeab,就是可以通过前述操作把A变成B的。
2、明白题意,笔者最开始觉得这种结构很熟悉,应该是队列的操作,可以定义一个队列,把A中的字母塞到队列中去,然后不断地取出首位,插入到末位,判断是不是能形成B字符串。
不过这样子还要定义一个队列,操作略显麻烦,直觉这样做并不是最简便的方法。
之后想到其实找到B首字母,比如上面给的例子,A是abcde,B是cdeab,B中首字母是c,那么找到c在A中的位置j,然后逐位比较A[j]和B[i](i从0开始)是否相等,这是A中后半部分的比较。
然后再比较A中前半部分是否与B中剩余部分相等。我们已知了j的位置,这些操作都是非常容易的。
这道题就可以做出来了。
不过,当碰到B中首字母在A中多次出现,而且首次出现还匹配不上,得第二次才匹配上的情况,比如A是abcdecdf,B是cdfabcde。
如果只是一次搜索,c在A中位置是第三位,这时匹配不成功。
但如果c在A中位置是倒数第三位那里,这时候的匹配就是成功的。
所以之前的做法就得在外面再加多个循环,一直迭代,直到搜索到能匹配的位置。
代码如下:(附详解)
bool rotateString(string A, string B)
{
int i=0,j=0,s1=A.size(),s2=B.size();
if(s1!=s2)//边界条件
return false;
if(s1==0)//边界条件,比如A和B都是空字符串
return true;
bool flag;
while(j<s1)//j用于表示B的首字母在A中的哪个位置
{
while(j<s1)//一直搜索,直到找到B中首字母在A中的位置
{
if(B[0]==A[j])
break;
j++;
}
flag=1;
i=0;
int t1=1,t2=j+1;//t1表示B中尝试匹配的起始位置,t2表示A中尝试匹配的起始位置
while(t2<s1)
{
if(B[t1]!=A[t2])
{
flag=0;
break;
}
t1++;
t2++;
}
if(flag==1)//如果上述匹配能成功,那么进行剩余部分的匹配
{
while(t1<s1)
{
if(B[t1]!=A[i])
{
flag=0;
break;
}
t1++;
i++;
}
}
if(flag==1)//如果两个部分都匹配成功了,那么返回true
return true;
j++;//如果没有匹配成功,那么j++,搜索B中首字母在A中的下一个出现位置
}
return false;//如果搜索到A的末尾,每一次都不能匹配成功,那么返回false
}
上述代码实测3ms,beats 97.87% of cpp submissions。
leetcode-796-Rotate String的更多相关文章
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- 796. Rotate String - LeetCode
Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...
- 【Leetcode_easy】796. Rotate String
problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...
- 【LeetCode】796. Rotate String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- 796. Rotate String旋转字符串
[抄题]: We are given two strings, A and B. A shift on A consists of taking string A and moving the lef ...
- 796. Rotate String
class Solution { public: bool rotateString(string A, string B) { if(A.length()==B.length()&& ...
- [LeetCode] 796. Rotate String_Easy **KMP
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...
- [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 ...
- LeetCode算法题-Rotate String(Java实现)
这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...
随机推荐
- 66. Plus One 数组加1
[抄题]: Given a non-negative integer represented as a non-empty array of digits, plus one to the integ ...
- mongodb修改用户名密码
首先先将启动mongo的配置文件里面的 auth:用户认证,改为false. 正确做法,利用db.changeUserPassword db.changeUserPassword('tank2','t ...
- win7 + eclipse + cocos2dx 开发环境配置
最近想在win7上配置eclipse+cocos2dx开发环境,在安装之前一定要注意每项是32位还是64位,我选择的都是64位版本的,闲话少叙我们开始安装吧! 1.下载cocos2dx,我选择的是co ...
- Java 读取jar内的文件的超简便方法
坑爹的java课程设计,偏要用jar来运行 读取.存储jar内文件的支持也好低 存储方法: 进入jar文件其实没有说的那么困难,jar文件本质是一个zip格式的压缩文件,只是把文件后缀名改了,要用Ja ...
- 23-tcp协议——TIME_WAIT状态和FIN_WAIT2状态
23-tcp协议——TIME_WAIT状态和FIN_WAIT2状态 摘自:https://blog.csdn.net/qq_35733751/article/details/80146161 2018 ...
- [GO]使用select实现超时
package main import ( "fmt" "time" ) func main() { ch := make(chan int) quit := ...
- [GO]使用select实现斐波那契
package main import "fmt" func fibonacci(ch chan <- int, quit <- chan bool) { x, y : ...
- YII2 rules 规则验证器
[['code','name'],'trim'], ['code','string','max'=>4], [['code','name','status'], 'required'], ['e ...
- UVa 1220 Party at Hali-Bula (树形DP,最大独立集)
题意:公司有 n 个人形成一个树形结构,除了老板都有唯一的一个直系上司,要求选尽量多的人,但不能同时选一人上和他的直系上司,问最多能选多少人,并且是不是唯一的方案. 析:这个题几乎就是树的最大的独立集 ...
- Spark 0.9.1和Shark 0.9.1分布式安装指南
目录 目录 1 1. 约定 1 2. 安装Scala 1 2.1. 下载 2 2.2. 安装 2 2.3. 设置环境变量 2 3. 安装Spark 2 3.1. 部署 2 3.2. 下载 3 3.3. ...