-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeatedStringMatch.cpp
43 lines (41 loc) · 969 Bytes
/
repeatedStringMatch.cpp
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
42
43
#include <bits/stdc++.h>
using namespace std;
bool isin(string a, string b){
int b1=0,a1=0, count = 0;
for(int i = 0; i < a.size(); i++){
if(a[a1] == b[b1]){
count++;
if(count == b.size()) return true;
b1++;
}
else if(a[a1] != b[b1]) {count = 0; b1=0;}
a1++;
}
return false;
}
string repeat(string a, string b){
return a+b;
}
int repeatedStringMatch(string A, string B)
{
// Your code goes here
int count = 1, i = 0;
string s = A;
if(isin(B, A)){
return count;
}
while(i < A.size()+B.size()){
if(isin(B,A)){
return count;
}
else{
A = repeat(A, s); count++;
}
i++;
}
return -1;
}
int main(){
cout<<repeatedStringMatch("abcd", "abcd");
return 0;
}