面试题 01.09. 字符串轮转

面试题 01.09. 字符串轮转

字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。

示例1:

 输入:s1 = "waterbottle", s2 = "erbottlewat"
 输出:True
示例2:

 输入:s1 = "aa", "aba"
 输出:False
提示:

字符串长度在[0, 100000]范围内。
说明:

你能只调用一次检查子串的方法吗?

思路

长度不一样返回false,然后s2 += s2, 在s2中找s1子串

class Solution {
public:
    bool isFlipedString(string s1, string s2) {
        int len1=s1.length(), len2=s2.length();
        if(len1!=len2) return false;
        s2 += s2;
        //检验s1是否在s2里面
        int j=0;
        for(int i=0;i
class Solution {
public:
    bool isFlipedString(string s1, string s2) {
        int len1=s1.length(), len2=s2.length();
        if(len1!=len2) return false;
        if((s2+s2).find(s1)!=string::npos) return true;
        else return false;
    }
};

 

你可能感兴趣的:(面试题 01.09. 字符串轮转)