-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatASubsequenceB.cpp
More file actions
49 lines (37 loc) · 897 Bytes
/
Copy pathRepeatASubsequenceB.cpp
File metadata and controls
49 lines (37 loc) · 897 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
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
bool isSubsequence(string s, string t){
int n = s.size();
int m = t.size();
int i = 0;
for(int j = 0; j<m && i<n; j++){
if(s[i] == t[j])
i++;
}
return i == n;
}
int repeatingCount(string A, string B){
// returns true if A repeated k times, then B is subsequence of A
int count = 1;
int LIMIT = 100;
int bLen = B.size();
string S = A;
while(S.size() < bLen){
S += A;
count++;
}
if(isSubsequence(B, S))
return count;
while(!isSubsequence(B, S) && count < LIMIT){
S = S + A;
count++;
}
return count == LIMIT? -1 : count;
}
int main()
{
string s = "abc";
string t = "ababababaaa";
cout<<repeatingCount(s, t);
return 0;
}