-
Notifications
You must be signed in to change notification settings - Fork 1
/
RepeatedSubsequence.java
123 lines (102 loc) · 3.62 KB
/
RepeatedSubsequence.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
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
/*
* https://www.techiedelight.com/longest-repeated-subsequence-problem/
*/
import java.util.Random;
import java.util.Map;
import java.util.HashMap;
class RepeatedSubsequence {
private static void checkInput(String X) {
if (X == null) {
throw new IllegalArgumentException();
}
}
private static int topDown(String X, String Y, int m, int n, Map<String, Integer> lookup) {
if (m == 0 || n == 0) {
return 0;
}
String key = m + "|" + n;
if (!lookup.containsKey(key)) {
int length = 0;
if (X.charAt(m - 1) == Y.charAt(n - 1) && m != n) {
length = 1 + topDown(X, Y, m - 1, n - 1, lookup);
} else {
length = Math.max(topDown(X, Y, m - 1, n, lookup), topDown(X, Y, m, n - 1, lookup));
}
lookup.put(key, length);
}
return lookup.get(key);
}
public static int topDown(String X) {
checkInput(X);
return topDown(X, X, X.length(), X.length(), new HashMap<String, Integer>());
}
private static int recursion(String X, String Y, int m, int n) {
if (m == 0 || n == 0) {
return 0;
}
if (X.charAt(m - 1) == Y.charAt(n - 1) && m != n) {
return 1 + recursion(X, Y, m - 1, n - 1);
}
return Math.max(recursion(X, Y, m , n - 1), recursion(X, Y, m - 1, n));
}
public static int recursion(String X) {
checkInput(X);
return recursion(X, X, X.length(), X.length());
}
private static String getSequence(String X, int[][] table,
int length, int m, int n) {
char[] sequence = new char[length];
while (m > 0 && n > 0) {
if (X.charAt(m - 1) == X.charAt(n - 1) && m != n) {
sequence[--length] = X.charAt(m - 1);
m--;
n--;
} else if(table[m - 1][n] > table[m][n - 1]) {
m--;
} else {
n--;
}
}
return new String(sequence);
}
private static void sequenceLength(String X, int[][] table) {
for (int i = 1; i <= X.length(); ++i) {
for (int j = 1; j <= X.length(); ++j) {
if (X.charAt(i - 1) == X.charAt(j - 1) && i != j) {
table[i][j] = 1 + table[i - 1][j - 1];
} else {
table[i][j] = Math.max(table[i][j- 1], table[i - 1][j]);
}
}
}
}
public static String getSequence(String X) {
checkInput(X);
int n = X.length();
int[][] table = new int[n + 1][n + 1];
sequenceLength(X, table);
return getSequence(X, table, table[n][n], n, n);
}
}
class Driver {
private static String generateString(int length, char[] alphabets) {
int n = alphabets.length;
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; ++i) {
int index = random.nextInt(n);
sb.append(alphabets[index]);
}
return sb.toString();
}
public static void main(String[] args) {
int length = 15;
char[] alphabets = {'A', 'T', 'G', 'C'};
String X = generateString(length, alphabets);
// String X = "ATACTCGGA";
System.out.println("X : " + X);
System.out.println("Using recursion : " + RepeatedSubsequence.recursion(X));
System.out.println("Using top down : " + RepeatedSubsequence.topDown(X));
System.out.println("Sequence : " + RepeatedSubsequence.getSequence(X));
}
}