![]() |
In Java, the Levenshtein Distance Algorithm is a pre-defined method used to measure the similarity between two strings and it can be used to calculate the minimum number of single-character edits (inserts, deletions, or substitutions) required to change one string into another. Prerequisites:
How Does this Algorithm Work?First, initialize the 2D array with the size of (m+1) * (n+1) where m and n are the lengths of the two input strings. Check the base cases if any one of the strings is empty then return the length of the other string. if(len1 != 0 & len2 != 0) then proceed to next steps After that initialize the first row and column of the 2D array with values representing the number of operation edits required to transform an empty string to the corresponding prefix of the input string. Measure the distances travel through the characters of both strings.
Print the minimum cost operations results then the value represents the Levenshtein distance between the two strings. Sample ProgramJava
Output
Levenshtein distance between "Java" and "JavaScript" is: 6 Explanation of the above Program:In the above example, the program calculates the Levenshtein distance between two strings:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |