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

• Last Updated : 19 Jan, 2021

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 deletions
• Number of insertions
• Number 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 m
• If m==0 return n
• Construct 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)); ` `    ``} ` `}`

Output

`2`

Time complexity: O(m*n).

My Personal Notes arrow_drop_up
Recommended Articles
Page :