Here you can find the source of levenshteinDistance(String st1, String st2)
Parameter | Description |
---|---|
st1 | a parameter |
st2 | a parameter |
public static int levenshteinDistance(String st1, String st2)
//package com.java2s; /*/*from w w w . ja v a2 s.c om*/ * Copyright 2009 Pedro Oliveira * This file is part of Incerto. * * Incerto is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Incerto is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Incerto. If not, see <http://www.gnu.org/licenses/>. */ public class Main { /** * Levensthein Distance between two Strings * @param st1 * @param st2 * @return */ public static int levenshteinDistance(String st1, String st2) { int n = st1.length(), m = st2.length(); if (n == 0) return m; if (m == 0) return n; int[][] distance = new int[n + 1][m + 1]; for (int i = 0; i <= n; i++) distance[i][0] = i; for (int j = 0; j <= m; j++) distance[0][j] = j; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) distance[i][j] = minimum(distance[i - 1][j] + 1, distance[i][j - 1] + 1, distance[i - 1][j - 1] + ((st1.charAt(i - 1) == st2.charAt(j - 1)) ? 0 : 1)); return distance[st1.length()][st2.length()]; } /** * Mininum of three integers * @param a * @param b * @param c * @return */ private static int minimum(int a, int b, int c) { return Math.min(Math.min(a, b), c); } }