This is a Java Program to implement Wagner Fischer Algorithm. Wagner–Fischer algorithm is a dynamic programming algorithm that measures the Levenshtein distance between two strings of characters.
Here is the source code of the Java Program to implement Wagner Fischer Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
/**
** Java Program to implement Wagner Fischer Algorithm
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
/** Class WagnerFischer **/
public class WagnerFischer
{
/** Function to get levenshtein distance between 2 strings **/
public int getLevenshteinDistance(String str1, String str2)
{
int len1 = str1.length();
int len2 = str2.length();
int[][] arr = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++)
arr[i][0] = i;
for (int i = 1; i <= len2; i++)
arr[0][i] = i;
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
int m = (str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0:1;
arr[i][j] = Math.min(Math.min(arr[i - 1][j] + 1, arr[i][j - 1] + 1), arr[i - 1][j - 1] + m);
}
}
return arr[len1][len2];
}
/** Main Function **/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Wagner Fischer Test\n");
/** Accept two strings **/
System.out.println("\nEnter string 1 :");
String str1 = br.readLine();
System.out.println("\nEnter string 2 :");
String str2 = br.readLine();
/** make object and call function **/
WagnerFischer wf = new WagnerFischer();
int lDist = wf.getLevenshteinDistance(str1, str2);
System.out.println("\nLevenshtein Distance = "+ lDist);
}
}
Wagner Fischer Test Enter string 1 : sunday Enter string 2 : saturday Levenshtein Distance = 3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.
Next Steps:
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for Information Technology Internship
- Buy Java Books
- Buy Programming Books
- Practice Information Technology MCQs
- Practice Programming MCQs