-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatedstringmatch.cpp
More file actions
41 lines (36 loc) · 909 Bytes
/
Copy pathrepeatedstringmatch.cpp
File metadata and controls
41 lines (36 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public:
bool ck(string a,string b){
int n=a.size();
int m=b.size();
for(int i=0;i<=n-m;i++)
{
int j;
for(j=0;j<m;j++)
{
if(a[i+j]!=b[j])
break;
}
if(j==m)
return 1;
}
return 0; //abcd cd
}
int repeatedStringMatch(string a, string b) {
int n=a.size();
int m=b.size();
string tp=a;
int ans=1;
while(n<m){
a+=tp;
ans++;
}
if(ck(a,b)){
return ans;
}
if(ck(a+tp,b)){
return ans+1;
}
return -1;
}
};