-
Notifications
You must be signed in to change notification settings - Fork 361
/
Divisibility of Repeated String
65 lines (48 loc) · 1.39 KB
/
Divisibility of Repeated String
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
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
Scanner bufferedReader = new Scanner(System.in);
System.out.println("Enter dividend string");
String s = bufferedReader.nextLine();
System.out.println("Enter divisor string");
String t = bufferedReader.nextLine();
int result = Result.findSmallestDivisor(s, t);
System.out.println(result);
}
}
class Result {
/*
* Complete the 'findSmallestDivisor' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. STRING s
* 2. STRING t
*/
public static int findSmallestDivisor(String s, String t) {
if(s.length() % t.length() > 0)
return -1;
StringBuilder sb = new StringBuilder();
for(int i=0;i*t.length() < s.length();i++) {
sb.append(t);
}
if(!sb.toString().equals(s))
return -1;
int divisible = 1;
for(int i=1;i<=t.length();i++) {
//optimized solution for skipping a few unrequired iterations
if(t.length()%divisible++ != 0) {
continue;
}
sb = new StringBuilder();
String subStr = t.substring(0, i);
while(sb.length() < t.length()) {
sb.append(subStr);
}
if(sb.toString().equals(t))
return i;
}
return -1;
}
}