Skip to content

Commit 7c6c91c

Browse files
committed
longest common subsequence solution
1 parent 75e9875 commit 7c6c91c

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int longestCommonSubsequence(String text1, String text2) {
3+
int m = text1.length();
4+
int n = text2.length();
5+
int[][] dp = new int[m + 1][n + 1];
6+
7+
for (int i = 1; i <= m; i++) {
8+
for (int j = 1; j <= n; j++) {
9+
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
10+
dp[i][j] = dp[i - 1][j - 1] + 1; // 문자가 같으면 대각선 값 + 1
11+
} else {
12+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); // 위 or 왼쪽 중 큰 값
13+
}
14+
}
15+
}
16+
return dp[m][n];
17+
}
18+
}

0 commit comments

Comments
 (0)