-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
29 lines (26 loc) · 901 Bytes
/
Copy pathsolution.java
File metadata and controls
29 lines (26 loc) · 901 Bytes
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
class Solution {
public static int minSuperSeq(String s1, String s2) {
int n = s1.length(), m = s2.length();
// Use the shorter string for columns to minimize space
if (m > n)
return minSuperSeq(s2, s1);
int[] prev = new int[m + 1];
int[] cur = new int[m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
cur[j] = 1 + prev[j - 1];
} else {
cur[j] = Math.max(prev[j], cur[j - 1]);
}
}
// move current row to prev and reset cur
int[] tmp = prev;
prev = cur;
cur = tmp;
java.util.Arrays.fill(cur, 0);
}
int lcs = prev[m];
return n + m - lcs;
}
}