题目描述:

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.

要完成的函数:

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的更多相关文章

  1. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  2. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  3. 【Leetcode_easy】796. Rotate String

    problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...

  4. 【LeetCode】796. Rotate String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. [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 ...

  6. 796. Rotate String旋转字符串

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

  7. 796. Rotate String

    class Solution { public: bool rotateString(string A, string B) { if(A.length()==B.length()&& ...

  8. [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 ...

  9. [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 ...

  10. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

随机推荐

  1. CS API 测试2

    //删除数据中心 http://192.168.150.16:8900/client/api?command=deleteZone&id=c2d4f46a-51af-4806-8378-4b3 ...

  2. OpenCV学习记录(一):使用haar分类器进行人脸识别 标签: opencv脸部识别c++ 2017-07-03 15:59 26人阅读

    OpenCV支持的目标检测的方法是利用样本的Haar特征进行的分类器训练,得到的级联boosted分类器(Cascade Classification).OpenCV2之后的C++接口除了Haar特征 ...

  3. stristr函数

  4. jquery设置select选中的文本

    <select id="prov">  <option value="1">北京市</option>  <option ...

  5. java Jvm工作原理学习笔记

    一.         JVM的生命周期 1.       JVM实例对应了一个独立运行的java程序它是进程级别 a)     启动.启动一个Java程序时,一个JVM实例就产生了,任何一个拥有pub ...

  6. FileAppender

    http://logback.qos.ch/manual/appenders.html#FileAppender <configuration> <appender name=&qu ...

  7. 常见的MIME

    "css": "text/css", "gif": "image/gif", "html": &qu ...

  8. git 只merge一个commit的方法

    https://git-scm.com/book/tr/v2/Git-Basics-Viewing-the-Commit-History gil log 来查看commit的记录 Other main ...

  9. spark介绍2

    上述结果是 map 1 filter 1 map 2 filter 2 map 3 filter 3 map 4 filter 4 即说明是并行,且互不干扰,每个task运行到最后

  10. [LeetCode 题解]: Permutation Sequcence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...