File tree Expand file tree Collapse file tree
longest-common-subsequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments