Java Program to Implement Wagner and Fisher Algorithm for Online String Matching

This is a java program to implement Wagner and Fisher Algorithm. In computer science, the Wagner–Fischer algorithm is a dynamic programming algorithm that computes the edit distance between two strings of characters.

Here is the source code of the Java Program to Implement Wagner and Fisher Algorithm for online String Matching. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7.  
  8. public class WagnerandFischer
  9. {
  10.     public int getLevenshteinDistance(String str1, String str2)
  11.     {
  12.         int len1 = str1.length();
  13.         int len2 = str2.length();
  14.         int[][] arr = new int[len1 + 1][len2 + 1];
  15.         for (int i = 0; i <= len1; i++)
  16.             arr[i][0] = i;
  17.         for (int i = 1; i <= len2; i++)
  18.             arr[0][i] = i;
  19.         for (int i = 1; i <= len1; i++)
  20.         {
  21.             for (int j = 1; j <= len2; j++)
  22.             {
  23.                 int m = (str1.charAt(i - 1) == str2.charAt(j - 1)) ? 0 : 1;
  24.                 arr[i][j] = Math.min(
  25.                         Math.min(arr[i - 1][j] + 1, arr[i][j - 1] + 1),
  26.                         arr[i - 1][j - 1] + m);
  27.             }
  28.         }
  29.         return arr[len1][len2];
  30.     }
  31.  
  32.     public static void main(String[] args) throws IOException
  33.     {
  34.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  35.         System.out.println("Enter string 1 :");
  36.         String str1 = br.readLine();
  37.         System.out.println("Enter string 2 :");
  38.         String str2 = br.readLine();
  39.         WagnerandFischer wf = new WagnerandFischer();
  40.         int lDist = wf.getLevenshteinDistance(str1, str2);
  41.         System.out.println("Edit (Levenshtein) Distance between two strings = "
  42.                 + lDist);
  43.         br.close();
  44.     }
  45. }

Output:

$ javac WagnerandFischer.java
$ java WagnerandFischer
 
Enter string 1 :
Wagner and Fisher Algorithm
Enter string 2 :
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel & Device Driver Programming. Our Founder has trained employees of almost all Top Companies in India. Here are few of them: VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium Networks, ST Microelectronics, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Northwest Bank, Mphasis, Tata Elxsi, Tata Communications, Mindtree, Cognizant, mid size IT companies and many Startups. Students from top Universities and colleges such as NIT Trichy, BITS Pilani, University of California, Irvine, University of Texas, Austin & PESIT Bangalore have benefited a lot from these courses as well. The assignments and real time projects for our courses are of extremely high quality with excellent learning curve.
Edit (Levenshtein) Distance between two strings = 844

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.