forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringoholics.java
More file actions
149 lines (114 loc) · 2.87 KB
/
Stringoholics.java
File metadata and controls
149 lines (114 loc) · 2.87 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package String;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Author - archit.s
* Date - 10/10/18
* Time - 1:17 PM
*/
public class Stringoholics {
final int M = (int) 1e9+7;
int maxLenSubString(String t){
int[] lps = new int[t.length()];
lps[0] = 0;
int len = 0;
int n = t.length();
int i =1;
int max= 0;
while(i<n){
if(t.charAt(i) == t.charAt(len)){
len++;
lps[i] = len;
i++;
max = Math.max(max,len);
}
else{
if(len == 0){
lps[i] = 0;
i++;
}
else{
len = lps[len-1];
}
}
}
return max;
}
long pow(long a, long p){
long ans = 1;
while(p>0){
if(p%2L == 1L){
ans = (ans * a)%M;
}
a = (a*a)%M;
p /= 2;
}
return ans%M;
}
void updateLcmMap(Map<Integer, Integer> m, Integer num){
int i = 2;
while(i<=num && i > 1){
int count = 0;
while(num % i == 0){
count++;
num /= i;
}
if(count == 0){
i++;
continue;
}
if(m.containsKey(i)){
int v = m.get(i);
if(v < count){
m.put(i,count);
}
}
else{
m.put(i,count);
}
i++;
}
}
long getLcm(ArrayList<Integer> lens){
Map<Integer, Integer> m = new HashMap<>();
for(Integer num : lens){
updateLcmMap(m, num);
}
long prod = 1;
for(Map.Entry<Integer, Integer> entry : m.entrySet()){
int k = entry.getKey();
int v = entry.getValue();
long p = pow(k,v) % M;
prod = (prod * p) % M;
}
return prod % M;
}
public int solve(ArrayList<String> A) {
ArrayList<Integer> lens = new ArrayList<>();
for(String t: A){
int maxLen = maxLenSubString(t);
int n = t.length();
if(n%(n-maxLen) == 0){
n -= maxLen;
}
long sum = 0;
int i =1;
do{
sum += i;
i++;
}while(sum % ((long) n) != 0L);
lens.add(i-1);
}
long lcm = getLcm(lens) % M;
return (int)lcm % M;
}
public static void main(String[] args) {
ArrayList<String> t = new ArrayList<String>(){{
add("a");
add("ababa");
add("aba");
}};
System.out.println(new Stringoholics().solve(t));
}
}