forked from lemonbashar/java-algo-expert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
76 lines (66 loc) · 2.28 KB
/
LongestCommonSubsequence.java
File metadata and controls
76 lines (66 loc) · 2.28 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
package algoexpert.hard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/*
PROBLEM:
Find the longest common subsequence of two strings
EXAMPLE:
"ZXVVYZW", "XKYKZPW" -> ["X", "Y", "Z", "W"]
Solution:
1. DP & build sequence : time : O(mn) | space : O(mn)
2. Recursion : time : O( 2 ^ (m + n) ) | space : O(m + n)
*/
public class LongestCommonSubsequence
{
public static void test()
{
System.out.println(longestCommonSubsequence("ZXVVYZW" , "XKYKZPW"));
System.out.println(longestCommonSubsequence("ABCDEFGH", "ABCDEFGH"));
}
public static ArrayList<Character> longestCommonSubsequence(String str1, String str2)
{
int[][] maxCommonTillMatrix = new int[str1.length() + 1][str2.length() + 1];
Arrays.fill(maxCommonTillMatrix[0], 0);
for (int row = 0; row < maxCommonTillMatrix.length; ++row)
{ maxCommonTillMatrix[row][0] = 0; }
// find max common count
for (int row = 1; row < maxCommonTillMatrix.length; ++row)
{
for (int col = 1; col < maxCommonTillMatrix[0].length; ++col)
{
if (str1.charAt(row - 1) == str2.charAt(col - 1))
{
maxCommonTillMatrix[row][col] = maxCommonTillMatrix[row - 1][col - 1] + 1;
}
else
{
maxCommonTillMatrix[row][col] = Integer.max(maxCommonTillMatrix[row - 1][col],
maxCommonTillMatrix[row][col - 1]);
}
}
}
// build sequence
ArrayList<Character> solution = new ArrayList<Character>();
int row = maxCommonTillMatrix.length - 1;
int col = maxCommonTillMatrix[0].length - 1;
while (row > 0 && col > 0)
{
if (str1.charAt(row - 1) == str2.charAt(col - 1))
{
solution.add(str1.charAt(row - 1));
row = row - 1;
col = col - 1;
}
else
{
if (maxCommonTillMatrix[row - 1][col] > maxCommonTillMatrix[row][col - 1])
{ row = row - 1; }
else
{ col = col - 1; }
}
}
Collections.reverse(solution);
return solution;
}
}