Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package util;
- import java.util.Arrays;
- public class EditDistance {
- // levenshtein distance with dp
- private static int costOfSubstitution(char a, char b) {
- return a == b ? 0 : 1;
- }
- private static int min(int... numbers) {
- return Arrays.stream(numbers)
- .min().orElse(Integer.MAX_VALUE);
- }
- private static int calculateRecursive(String x, String y) {
- if (x.isEmpty()) {
- return y.length();
- }
- if (y.isEmpty()) {
- return x.length();
- }
- int substitution = calculate(x.substring(1), y.substring(1))
- + costOfSubstitution(x.charAt(0), y.charAt(0));
- int insertion = calculate(x, y.substring(1)) + 1;
- int deletion = calculate(x.substring(1), y) + 1;
- return min(substitution, insertion, deletion);
- }
- public static int calculate(String x, String y) {
- int[][] dp = new int[x.length() + 1][y.length() + 1];
- for (int i = 0; i <= x.length(); i++) {
- for (int j = 0; j <= y.length(); j++) {
- if (i == 0) {
- dp[i][j] = j;
- }
- else if (j == 0) {
- dp[i][j] = i;
- }
- else {
- dp[i][j] = min(dp[i - 1][j - 1]
- + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)),
- dp[i - 1][j] + 1,
- dp[i][j - 1] + 1);
- }
- }
- }
- return dp[x.length()][y.length()];
- }
- public static void main(String[] args) {
- System.out.println(calculate("kitten", "sitting"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement