-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
46 lines (37 loc) · 1.17 KB
/
Solution.java
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
package _686;
public class Solution {
public int repeatedStringMatch(String A, String B) {
if (A.length() > B.length()) {
return A.contains(B) ? 1 : (A + A).contains(B) ? 2 : -1;
}
int aInB = B.indexOf(A);
if (aInB >= A.length()) {
return -1;
}
if (aInB == -1) {
return (A + A).contains(B) ? 2 : -1;
}
int repeatCount = aInB == 0 ? 0 : 1;
int iA = A.length() - aInB;
int iB = 0;
while (iB < B.length()) {
if (iA == A.length()) {
iA = 0;
repeatCount++;
}
if (A.charAt(iA) != B.charAt(iB)) {
return -1;
}
iA++;
iB++;
}
return repeatCount;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.repeatedStringMatch("abcd", "cdabcdab"));
System.out.println(solution.repeatedStringMatch("abcd", "ab"));
System.out.println(solution.repeatedStringMatch("abcd", "da"));
System.out.println(solution.repeatedStringMatch("a", "aa"));
}
}