Here you can find the source of calculateDistance(String firstString, String secondString)
Parameter | Description |
---|---|
firstString | First given string |
secondString | Second given string |
private static int calculateDistance(String firstString, String secondString)
//package com.java2s; //License from project: Open Source License public class Main { /**//from w w w. jav a 2s . com * Calculate the edit distance between two strings. This method calculates * the Levenshtein distance using dynamic programming. * * @param firstString * First given string * @param secondString * Second given string * @return The edit distance between the two strings, the higher it is the * more do they differ */ private static int calculateDistance(String firstString, String secondString) { firstString = firstString.toLowerCase(); secondString = secondString.toLowerCase(); int[] costs = new int[secondString.length() + 1]; for (int j = 0; j < costs.length; j++) costs[j] = j; for (int i = 1; i <= firstString.length(); i++) { costs[0] = i; int nw = i - 1; for (int j = 1; j <= secondString.length(); j++) { int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), firstString.charAt(i - 1) == secondString.charAt(j - 1) ? nw : nw + 1); nw = costs[j]; costs[j] = cj; } } return costs[secondString.length()]; } }