Skip to content

Commit da77602

Browse files
committed
add solution for LCS
1 parent bd4afe0 commit da77602

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class Solution {
3+
public int longestCommonSubsequence(String text1, String text2) {
4+
5+
6+
//int longerlen= (text1.length() >= text2.length()) ? text1.length() : text2.length();
7+
//int lesslen= (text1.length() <= text2.length()) ? text1.length() : text2.length();
8+
int[][] dp = new int[text1.length()+1][text2.length()+1];
9+
10+
//String longertext= (text1.length() >= text2.length()) ? text1 : text2;
11+
//String lesstext= (text1.length() <= text2.length()) ? text1 : text2;
12+
13+
14+
15+
for (int i = 1; i <=text1.length(); i++) {
16+
for (int j = 1; j <=text2.length(); j++) {
17+
//문자가 같으면
18+
if (text1.charAt(i-1) == text2.charAt(j-1)){
19+
dp[i][j] = dp[i-1][j-1]+1;
20+
//System.out.println("dp["+i+"]["+j+"]: "+ dp[i][j]);
21+
}else{
22+
//문자가 다르면
23+
dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
24+
//System.out.println("dp["+i+"]["+j+"]: "+ dp[i][j]);
25+
}
26+
}
27+
}
28+
29+
30+
return dp[text1.length()][text2.length()];
31+
}//end of method
32+
}
33+

0 commit comments

Comments
 (0)