面试题 01.09. 字符串轮转

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

示例1:

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

输入:s1 = "aa", s2 = "aba"
输出:False

1.原创

class Solution {
public:
    bool isFlipedString(string s1, string s2) {
        int m = s1.size(), n = s2.size();
        if(m != n) return false;
        if(m == 0) return true;
        for(int i = 0; i < m; i ++){
            if(s1[i] == s2[0]){
                if(s1.substr(0, i) == s2.substr(m - i) && s1.substr(i) == s2.substr(0, m - i))
                    return true;
            }
        }
        return false;
    }
};

2.题解

class Solution {
public:
    bool isFlipedString(string s1, string s2) {
        return s1.size()==s2.size()&&(s1+s1).find(s2)!=-1;
    }
};

作者:yzwz
链接:https://leetcode-cn.com/problems/string-rotation-lcci/solution/yi-xing-jiu-gou-liao-by-yzwz/
上一篇:mysql 截取字符串


下一篇:Python算法获取最长不重复子串