Saturday, September 30, 2017
686. Repeated String Match
bool isSubStr(int n, string& A, string& B) {
if(n*A.size()<B.size()) return false;
string a = "";
for(int i=0; i<n; i++) a += A;
for(int i=0; i<A.size(); i++) {
int j;
for(j=0; j<B.size(); j++) {
if(a[i+j] != B[j]) break;
}
if(j==B.size()) return true;
}
return false;
}
int repeatedStringMatch(string A, string B) {
if(B.size() == 0) return 1;
if(A.size() == 0) return -1;
int na = A.size(), nb = B.size();
int k = nb/na;
for(int n=k; n<=k+2; n++) {
if(isSubStr(n, A, B)) return n;
}
return -1;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment