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.

package com.maixuanviet.setandstring;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class WagnerandFischer
{
    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];
    }
 
    public static void main(String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter string 1 :");
        String str1 = br.readLine();
        System.out.println("Enter string 2 :");
        String str2 = br.readLine();
        WagnerandFischer wf = new WagnerandFischer();
        int lDist = wf.getLevenshteinDistance(str1, str2);
        System.out.println("Edit (Levenshtein) Distance between two strings = "
                + lDist);
        br.close();
    }
}

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

Related posts:

Apache Commons Collections BidiMap
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
How to Add a Single Element to a Stream
Java Program to Implement Queue
Spring Boot - Runners
Java Program to Implement Affine Cipher
Comparing Long Values in Java
Java Program to Generate a Random Subset by Coin Flipping
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Compare Binary and Sequential Search
Java Program to Implement Double Order Traversal of a Binary Tree
Java Program to Find Nearest Neighbor Using Linear Search
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Spring Boot - OAuth2 with JWT
Guide to Java 8’s Collectors
Java Program to Perform Addition Operation Using Bitwise Operators
Spring WebClient and OAuth2 Support
Java Program to Implement Naor-Reingold Pseudo Random Function
Debug a JavaMail Program
Working With Maps Using Streams
Java Program to Implement Binomial Heap
Spring Security Remember Me
Hướng dẫn sử dụng lớp Console trong java
Adding Parameters to HttpClient Requests
Java Program to Find Inverse of a Matrix
Java Program to Perform Partition of an Integer in All Possible Ways
How to Manually Authenticate User with Spring Security
Auditing with JPA, Hibernate, and Spring Data JPA
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Check Whether a Given Point is in a Given Polygon
Spring 5 Testing with @EnabledIf Annotation