简单解法 时间复杂度为find的复杂度O(m * n) Space = O(n)

class Solution {
public:
int repeatedStringMatch(string A, string B) {
int res = 1;
string C(A);
while(C.length() < B.length()) {
C.append(A);
++res;
}
if(C.find(B) != string::npos)
return res;
C.append(A);
if(C.find(B) != string::npos)
return res + 1;
return -1;
}
};

KMP解法

posted on 2017-10-23 14:15  bloomingFlower  阅读(170)  评论(0编辑  收藏  举报