深信服笔试题-字符串编辑距离
思路:
• 字符串的编辑距离(Levenshtein距离)算法;
• 详情见百度。
class Solution { public int function(String a, String b){ int n1 = a.length(), n2 = b.length(); int[][] dp = new int[n1+1][n2+1]; for(int i = 1; i <= n1; i++) dp[i][0] = i; for(int i = 1; i <= n2; i++) dp[0][i] = i; for(int i = 1; i <= n1; i++){ for(int j = 1; j <= n2; j++){ if(a.charAt(i-1) == b.charAt(j-1)) dp[i][j] = dp[i-1][j-1]; else dp[i][j] = Math.min(dp[i-1][j-1], Math.min(dp[i][j-1], dp[i-1][j])) + 1; } } return dp[n1][n2]; } }
结果:提交 OJ,显示 AC.