-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum_repeat_to_make_substring.cpp
59 lines (44 loc) · 1.14 KB
/
Minimum_repeat_to_make_substring.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution {
public:
int minRepeats(string s1, string s2) {
string temp = s1;
int cnt = 1;
if (s1 == s2) return cnt;
if(s1.length() >= s2.length())
{
s1 += s1;
cnt++;
if (s2.find(s1) == string::npos) return cnt;
}
if (s2.find(s1) == string::npos) return -1;
while (s1.length() < s2.length())
{
s1 += temp;
cnt++;
}
if (s1.find(s2) != string::npos) return cnt;
s1 += temp;
cnt++;
if (s1.find(s2) != string::npos) return cnt;
return -1;
}
};
//{ Driver Code Starts.
int main() {
int t;
scanf("%d ", &t);
while (t--) {
string A, B;
getline(cin, A);
getline(cin, B);
Solution ob;
cout << ob.minRepeats(A, B) << endl;
}
return 0;
}
// } Driver Code Ends