Repository navigation
Expand file tree
/
Copy pathShortestCommonSubsequence.java
More file actions
53 lines (50 loc) · 1.67 KB
/
Copy pathShortestCommonSubsequence.java
File metadata and controls
53 lines (50 loc) · 1.67 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
import Interfaces.CommonSubsequence;
import java.util.ArrayList;
import java.util.List;
public class ShortestCommonSubsequence implements CommonSubsequence {
public List<String> commonSubsequence(String s, String t, int m, int n) {
ArrayList<String> result = new ArrayList<>();
int[][] table = new int[m + 2][n + 2];
int tableMin = 10000;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (s.charAt(i) == t.charAt(j)) {
table[i + 1][j + 1] = table[i][j] + 1;
if (table[i + 1][j + 1] < tableMin) {
tableMin = table[i + 1][j + 1];
}
}
}
}
int index = tableMin;
int temp = index;
char[] scs = new char[index];
int i = 0, j = 0;
StringBuilder shortest = new StringBuilder();
while(i <= m && j <= n){
if(index > 0) {
if (s.charAt(i) == t.charAt(j)) {
scs[index - 1] = s.charAt(i);
i = 10000;
j = 10000;
index--;
} else if (table[i][j + 1] > table[i + 1][j])
i++;
else
j++;
}
else{
i = m+1;
j= n +1;
}
}
for(int k=0; k<temp; k++){
if (scs[k] != '\u0000') {
shortest.append(scs[k]);
}
}
result.add(shortest.toString());
result.add(String.valueOf(tableMin));
return result;
}
}