最长公共子串和最长公共子序列是两个不一样的问题,前者要求连续出现在两个字符串中,后者只要顺序一致即可。两者的解决方法类似,都是使用动态规划(DP)。
最长公共子序列:
状态转移方程:
package LeetCode;
public class LongestCommonSubsequence {
public static int findLongestCommonSubsequence(String str1, String str2) {
int m = str1.length(), n = str2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (str1.charAt(i) == str2.charAt(j))
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = Math.max(dp[i + 1][j], dp[i][j + 1]);
}
}
return dp[m][n];
}
public static String findLongestCommonSubsequence1(String str1, String str2) {
int n = str1.length(), m = str2.length();
String[][] dp = new String[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++)
dp[i][j] = "";
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (str1.charAt(i) == str2.charAt(j))
dp[i + 1][j + 1] = dp[i][j] + str1.charAt(i);
else {
if (dp[i + 1][j].length() > dp[i][j + 1].length())
dp[i + 1][j + 1] = dp[i + 1][j];
else
dp[i + 1][j + 1] = dp[i][j + 1];
}
}
}
return dp[m][n];
}
//计算Levenshtein Distance
public static int calLevenshteinDistance(String str1, String str2, int i, int j) {
if (i == 0)
return j;
if (j == 0)
return i;
int cost = str1.charAt(i - 1) == str2.charAt(j - 1) ? 0 : 1;
return Math.min(Math.min(calLevenshteinDistance(str1, str2, i - 1, j) + 1, calLevenshteinDistance(str1, str2, i, j - 1) + 1),
calLevenshteinDistance(str1, str2, i - 1, j - 1) + cost);
}
public static int LevenshteinDistance1(String s1, String s2) {
int substitutionCost;
int m = s1.length(), n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++)
dp[i][0] = i;
for (int i = 0; i <= n; i++)
dp[0][i] = i;
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1))
substitutionCost = 0;
else
substitutionCost = 1;
dp[i][j] = Math.min(Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1), dp[i - 1][j - 1] + substitutionCost);
}
}
return dp[m][n];
}
public static void main(String[] args) {
String str1 = "abcde";
String str2 = "abgde";
System.out.println(findLongestCommonSubsequence(str1, str2));
System.out.println(findLongestCommonSubsequence1(str1, str2));
str1 = "kitten";
str2 = "sitting";
System.out.println(calLevenshteinDistance(str1, str2, str1.length(), str2.length()));
System.out.println(LevenshteinDistance1(str1, str2));
}
}
最长公共子串:
状态转移方程:
package LeetCode;
import java.util.Arrays;
public class LongestCommonSubstring {
public static int findLongestCommonSubstring(String str1,String str2){
int m=str1.length(),n=str2.length();
int[][] dp=new int[m+1][n+1];
for (int i=0;i<m;i++){
for(int j=0;j<n;j++){
if (str1.charAt(i) == str2.charAt(j))
dp[i + 1][j + 1] = dp[i][j] + 1;
else
dp[i + 1][j + 1] = 0;
}
}
return dp[m][n];
}
public static String findLongestCommonSubstring1(String str1,String str2){
int m=str1.length(),n=str2.length();
String[][] StringDP=new String[m+1][n+1];
for (int i=0;i<=m;i++)
Arrays.fill(StringDP[i],"");
for (int i=0;i<m;i++){
for(int j=0;j<n;j++){
if (str1.charAt(i) == str2.charAt(j))
StringDP[i+1][j+1]=StringDP[i][j]+str1.charAt(i);
}
}
String ans="";
for(int i=0;i<=m;i++){
if(StringDP[i][i].length()>ans.length())
ans=StringDP[i][i];
}
return ans;
}
public static void main(String[] args){
String str1="abcde";
String str2="abgde";
System.out.println(findLongestCommonSubstring(str1,str2));
System.out.println(findLongestCommonSubstring1(str1,str2));
}
}
这两个问题的转移方程几乎差不多,两个问题都带寻找对应字符串的动态规划代码。
72. Edit Distance(https://leetcode.com/problems/edit-distance/)
class Solution {
public int minDistance(String word1, String word2) {
int m=word1.length(),n=word2.length();int insert=0,del=0,rep=0;
int[][] Edit=new int[m+1][n+1];
for(int j=0;j<n+1;j++)
Edit[0][j]=j;
for(int i=1;i<m+1;i++)
{
Edit[i][0]=i;
for(int j=1;j<n+1;j++)
{
insert=Edit[i-1][j]+1;
del=Edit[i][j-1]+1;
if(word1.charAt(i-1)==word2.charAt(j-1))
rep=Edit[i-1][j-1];
else
rep=Edit[i-1][j-1]+1;
Edit[i][j]=Math.min(Math.min(insert,del),rep);
}
}
return Edit[m][n];
}
}
参考文献:《算法导论》