1. 程式人生 > >leetcode686+重複A是否有字串B,重複有規律,重複一定就不用重複了

leetcode686+重複A是否有字串B,重複有規律,重複一定就不用重複了

https://leetcode.com/problems/repeated-string-match/description/

//A比B的長度要短,這時A要不斷增加。  2.  A 比 B 剛好長,
//3、A 比 B 剛好長的長度 + 1個A的長度, 這時能夠保證在A的原始的串中能夠索引完一遍B的長度,如果此時沒有找到那麼就要返回-1
class Solution {
public:
    int repeatedStringMatch(string A, string B) {
        int res = 1;
        string tmp = A;
        while(tmp.size() < B.size()){
            tmp += A;
            res +=1;
        }
        if(tmp.find(B)!=-1)return res;
        tmp += A, res+=1;
        if(tmp.find(B)!=-1) return res;
        return -1;
    }
};