-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest repeating and non-overlapping substring
58 lines (45 loc) · 1.49 KB
/
Longest repeating and non-overlapping substring
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
package gfg;
//Longest repeating and non-overlapping substring
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
String S = read.readLine();
Solution ob = new Solution();
System.out.println(ob.longestSubstring(S, N));
}
}
}
class Solution {
static String longestSubstring(String s, int n) {
// code here
int [][] mat = new int [n + 1][n + 1];
int ans_length = 0;
int idx = 0;
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
if(s.charAt(i - 1) == s.charAt(j - 1) && mat[i - 1][j - 1] + 1 <= (j - i)) {
mat[i][j] = mat[i - 1][j - 1] + 1;
if(mat[i][j] > ans_length) {
ans_length = mat[i][j];
idx = i - 1;
}
}
}
}
StringBuilder ans = new StringBuilder();
if(ans_length > 0) {
for(int i = idx - ans_length + 1; i <= idx; i++) {
ans.append(s.charAt(i));
}
}
else {
ans.append("-1");
}
return ans.toString();
}
};