import java.util.*;
public class Solution {
public String LCS (String str1, String str2) {
int[][] dp = new int[str1.length() + 1][str2.length() + 1];
int max = 0;
int pos = 0;
for(int i = 1; i < str1.length() + 1; i++){
for(int j = 1; j < str2.length() + 1; j++){
if(str1.charAt(i - 1) == str2.charAt(j - 1)){
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
dp[i][j] = 0;
}
if(dp[i][j] > max){
max = dp[i][j];
pos = i - 1;
}
}
}
return str1.substring(pos - max + 1, pos + 1);
}
}
- 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