796 LeetCode 旋转字符串

题目描述:
在这里插入图片描述
思路:
首先找到A中与B的第一个字符相等的位置,从这个位置开始查找,比较

代码如下:

class Solution {
public:
    bool rotateString(string A, string B) {
        if(A.size()!=B.size())  return false;
        if(A.size()==0&&B.size()==0)   return true;
        vector<int>same;
        for(int i=0;i<A.size();i++){
            if(A[i]==B[0])
            same.push_back(i);
        }
        for(int i=0;i<same.size();i++){
            int j=0;
            int temp=same[i];
            while(j<B.size()){
                if(B[j]==A[temp]){
                j++;
                temp++;
                if(temp==A.size())
                temp-=A.size();}
                else break;
            }
            if(j==B.size())
            return true;
        }
        return false;
    }
};
发布了123 篇原创文章 · 获赞 0 · 访问量 949

猜你喜欢

转载自blog.csdn.net/peachzy/article/details/104376121
今日推荐