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:
How to Convert List to Map in Java
Java Program to Check whether Undirected Graph is Connected using BFS
A Guide to Concurrent Queues in Java
Java Program to Implement Interval Tree
How to Replace Many if Statements in Java
HTTP Authentification and CGI/Servlet
Daemon Threads in Java
Quick Guide to Spring Bean Scopes
Giới thiệu Java 8
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to Implement Hash Tables Chaining with Binary Trees
Hướng dẫn Java Design Pattern – MVC
Java Byte Array to InputStream
A Guide to EnumMap
Java Program to Implement Range Tree
Immutable Map Implementations in Java
Java Program to Implement AttributeList API
Ignore Null Fields with Jackson
Debugging Reactive Streams in Java
Hướng dẫn sử dụng Printing Service trong Java
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Implement CountMinSketch
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Check whether Graph is a Bipartite using BFS
Logout in an OAuth Secured Application
Dockerizing a Spring Boot Application
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Constructor Injection in Spring with Lombok
Lớp Collections trong Java (Collections Utility Class)
Hướng dẫn Java Design Pattern – Builder
Primitive Type Streams in Java 8