Skip to content

Commit 034eb59

Browse files
author
github-actions
committed
Format: Google java formatter
1 parent 8d4a4a2 commit 034eb59

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

DynamicProgramming/LongestCommonSubsequence/Solution.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/**
99
* * Longest Common Subsequence (LCS) Problem
1010
*/
11-
1211
public class Solution {
1312
public int longestCommonSubsequence(String s1, String s2) {
1413
// return longestCommonSubsequenceRec(
@@ -19,7 +18,8 @@ public int longestCommonSubsequence(String s1, String s2) {
1918
// int[][] cache = new int[s1.length() + 1][s2.length() + 1];
2019
// for (int[] row : cache) Arrays.fill(row, -1);
2120

22-
// return longestCommonSubsequenceMem(s1.length(), s2.length(), s1.toCharArray(), s2.toCharArray(), cache);
21+
// return longestCommonSubsequenceMem(s1.length(), s2.length(), s1.toCharArray(),
22+
// s2.toCharArray(), cache);
2323

2424
return longestCommonSubsequenceDP(s1, s2);
2525

@@ -28,7 +28,7 @@ public int longestCommonSubsequence(String s1, String s2) {
2828

2929
/**
3030
* * Dynamic Programming Approach (Space Optimized)
31-
*
31+
*
3232
* * TC: O(mn)
3333
* * SC: O(n)
3434
*/
@@ -38,7 +38,7 @@ public int longestCommonSubsequence(String s1, String s2) {
3838

3939
// for (int i = 1; i < m + 1; i++) {
4040
// // Calculate whether the the
41-
// // current row is even/odd.
41+
// // current row is even/odd.
4242
// idx = i & 1;
4343
// for (int j = 1; j < n + 1; j++) {
4444
// if (s1.charAt(i - 1) == s2.charAt(j - 1))
@@ -56,7 +56,7 @@ public int longestCommonSubsequence(String s1, String s2) {
5656

5757
/**
5858
* * Dynamic Programming Approach
59-
*
59+
*
6060
* * TC: O(mn)
6161
* * SC: O(mn)
6262
*/
@@ -66,13 +66,8 @@ private int longestCommonSubsequenceDP(String s1, String s2) {
6666

6767
for (int i = 1; i < m + 1; i++) {
6868
for (int j = 1; j < n + 1; j++) {
69-
if (s1.charAt(i - 1) == s2.charAt(j - 1))
70-
dp[i][j] = 1 + dp[i - 1][j - 1];
71-
else
72-
dp[i][j] = Math.max(
73-
dp[i - 1][j],
74-
dp[i][j - 1]
75-
);
69+
if (s1.charAt(i - 1) == s2.charAt(j - 1)) dp[i][j] = 1 + dp[i - 1][j - 1];
70+
else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
7671
}
7772
}
7873

@@ -91,8 +86,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
9186
seq.add(0, String.valueOf(s1.charAt(i - 1)));
9287
--i;
9388
--j;
94-
}
95-
else if (dp[i - 1][j] > dp[i][j - 1]) --i;
89+
} else if (dp[i - 1][j] > dp[i][j - 1]) --i;
9690
else --j;
9791
}
9892

@@ -101,7 +95,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
10195

10296
/**
10397
* * Memoization Approach
104-
*
98+
*
10599
* * TC: O(mn)
106100
* * SC: O(mn)
107101
*/
@@ -121,7 +115,7 @@ private String buildSequence(String s1, String s2, int[][] dp) {
121115

122116
/**
123117
* * Recursive Approach
124-
*
118+
*
125119
* * TC: O(3^(m + n)) approximately
126120
* * SC: O(3^(m + n)) approximately
127121
*/
@@ -146,4 +140,4 @@ public static void main(String[] args) {
146140
// should be 4
147141
System.out.println(solution.longestCommonSubsequence("AGGTAB", "GXTXAYB"));
148142
}
149-
}
143+
}

0 commit comments

Comments
 (0)