-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay28.java
More file actions
99 lines (81 loc) · 2.43 KB
/
Day28.java
File metadata and controls
99 lines (81 loc) · 2.43 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*Minimum repeat to make substring
Given two strings s1 and s2. Return a minimum number of times s1 has to be repeated such that s2 is a substring of it. If s2 can never be a substring then return -1.
Note: Both the strings contain only lowercase letters.
Examples:
Input: s1 = "ww", s2 = "www"
Output: 2
Explanation: Repeating s1 two times "wwww", s2 is a substring of it.
Input: s1 = "abac", s2 = "cabaca"
Output: 3
Explanation: Repeating s1 three times "abacabacabac", s2 is a substring of it. s2 is not a substring of s1 when it is repeated less than 3 times.
Input: s1 = "ab", s2 = "cab"
Output: -1
Explanation: No matter how many times we repeat s1, we can't get a string such that s2 is a substring of it. */
/*class Solution {
public int repeatedStringMatch(String s1, String s2) {
StringBuilder sb = new StringBuilder();
int count = 0;
while (sb.length() < s2.length()) {
sb.append(s1);
count++;
}
if (kmpSearch(sb.toString(), s2)) {
return count;
}
sb.append(s1);
count++;
if (kmpSearch(sb.toString(), s2)) {
return count;
}
return -1;
}
// KMP Pattern Search
private boolean kmpSearch(String text, String pattern) {
int[] lps = buildLPS(pattern);
int i = 0, j = 0;
while (i < text.length()) {
if (text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
if (j == pattern.length()) {
return true;
}
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return false;
}
// Build LPS Array
private int[] buildLPS(String pattern) {
int[] lps = new int[pattern.length()];
int len = 0;
int i = 1;
while (i < pattern.length()) {
if (pattern.charAt(i) == pattern.charAt(len)) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
}
*/
/*⏱️ Complexity (Optimized)
Time: O(n + m)
Space: O(m)
Where:
n = length of repeated string
m = length of s2 */