-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeatedStringMatching.java
54 lines (54 loc) · 1.25 KB
/
RepeatedStringMatching.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
47
48
49
50
51
52
53
54
public class RepeatedStringMatching {
public static void main(String[] args) {
System.out.println(repeatedStringMatch("abc","cabcabca"));
}
public static int repeatedStringMatch(String a, String b) {
int repeat = 1;
String test = a+"";
while((a.length())<b.length())
{
repeat++;
a += test;
}
int count=0;
while(!isSubstring(a,b))
{
if(count>3)
{
return -1;
}
repeat++;
count++;
a+=a;
}
if(!isSubstring(a,b))
{
return -1;
}
return repeat;
}
public static boolean isSubstring(String a, String b)
{
boolean found = false;
for(int i=0;i<=(a.length()-b.length());i++)
{
for(int j=i,k=0;k<b.length();k++,j++)
{
if(a.charAt(j)==b.charAt(k))
{
found = true;
}
else
{
found = false;
break;
}
}
if(found)
{
return true;
}
}
return found;
}
}