Repeated String Match

2017/12/6 posted in  leetcode

要考虑"abc" "abc" "abc""c abc a"的情况,所以i<=lenB/lenA+2
c++
class Solution {
public:
int repeatedStringMatch(string A, string B) {
int lenA = A.length(),lenB = B.length();
string tmp = A;
for (int i = 1; i<=lenB/lenA+2; i++ , tmp+=A) {
if (tmp.find(B)!=string::npos) {
return i;
}
}
return -1;
}
};