Java Program to Implement Wagner and Fisher Algorithm for Online String Matching Last Updated : 19 Jan, 2021 Comments Improve Suggest changes 1 Likes Like Report The Wagner-Fischer Algorithm is a dynamic programming algorithm that measures the Levenshtein distance or the edit distance between two strings of characters. Levenshtein Distance(LD) calculates how similar are the two strings. The distance is calculated by three parameters to transform the string1 to string2. The parameters are: Number of deletionsNumber of insertionsNumber of Substitutions For example Input: str1="cat" str2="cat" Output: 0 The levenshtein distance i.e. LD(str1,str2)=0, because both the strings are equal and no changes are needed. Input: str1="bat" str2="cat" Output: 1 The levenshtein distance i.e. LD(str1,str2)=1, because we need to substitute 'b' with 'c' to transform the string1 to string2. Input: str1="bat" str2="ball" Output: 2 The levenshtein distance i.e. LD(str1,str2)=2, because there is one substitution of 't' to 'l' and one insertion of 'l' needed to transform "bat" to "ball". Algorithm : Store lengths of both the strings str1 and str2 in some variables say n and m respectively.If n==0 return mIf m==0 return nConstruct a matrix of m rows and n columns and initialize the first row to 0 to n and the first column to 0 to m.Check each character of str1 and each character to str2.If the character at str1[i] is equal to the character at str2[j] then m is 0 and if both are not equal then m is 1.Set the element at arr[i][j] of the matrix as the minimum of the following: (arr[i-1][j]+1, arr[i][j-1]+1, arr[i-1][j-1]+m)After iterating through the steps 5, 6 ,7 ,the distance is found at arr[n][m]. Code : Java // Java Program to Implement Wagner and Fisher // Algorithm for online String Matching import java.util.*; class GFG { public static int getDistance(String str1, String str2) { int l1 = str1.length(); int l2 = str2.length(); if (l1 == 0) return l2; if (l2 == 0) return l1; int arr[][] = new int[l1 + 1][l2 + 1]; for (int i = 0; i <= l1; i++) arr[i][0] = i; for (int j = 0; j <= l2; j++) arr[0][j] = j; for (int i = 1; i <= l1; i++) { char ch1 = str1.charAt(i - 1); for (int j = 1; j <= l2; j++) { char ch2 = str2.charAt(j - 1); int m = ch1 == ch2 ? 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[l1][l2]; } public static void main(String[] args) { String str1, str2; str1 = "bat"; str2 = "ball"; System.out.println(getDistance(str1, str2)); } } Output2 Time complexity: O(m*n). Create Quiz Comment N namitachaudhary60 Follow 1 Improve N namitachaudhary60 Follow 1 Improve Article Tags : Java Java Programs Java-String-Programs Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like