Java Distance Calculate distance(String a, String b)

Here you can find the source of distance(String a, String b)

Description

distance

License

Open Source License

Declaration

private static int distance(String a, String b) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static int distance(String a, String b) {
        a = a.toLowerCase();// ww  w . j  av a  2s .  com
        a = a.replaceAll("[^a-zA-Z0-9]", "");
        b = b.toLowerCase();
        b = b.replaceAll("[^a-zA-Z0-9]", "");
        // i == 0
        int[] costs = new int[b.length() + 1];
        for (int j = 0; j < costs.length; j++) {
            costs[j] = j;
        }
        for (int i = 1; i <= a.length(); i++) {
            // j == 0; nw = lev(i - 1, j)
            costs[0] = i;
            int nw = i - 1;
            for (int j = 1; j <= b.length(); j++) {
                int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]),
                        a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
                nw = costs[j];
                costs[j] = cj;
            }
        }
        return costs[b.length()];
    }
}

Related

  1. distance(int x1, int y1, int x2, int y2)
  2. distance(int x1, int y1, int x2, int y2)
  3. distance(int x1, int y1, int x2, int y2)
  4. distance(int xa, int ya, int xb, int yb)
  5. distance(int[] a, int[] b)
  6. distance(String coord1, String coord2, char unit)
  7. distance(String seq1, String seq2)
  8. distance2(float x, float y, float x1, float y1)
  9. distance2(float x1, float y1, float x2, float y2)