-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pushing edit distance program in Java
- Loading branch information
Kumar Laxmikant
authored and
Kumar Laxmikant
committed
Oct 4, 2024
1 parent
3e51541
commit 9840c53
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
public class GfG { | ||
// A Naive recursive Java program to find minimum number | ||
// of operations to convert s1 to s2 | ||
public static int editDistRec(String s1, String s2, int m, int n) { | ||
// If first string is empty, the only option is to | ||
// insert all characters of second string into first | ||
if (m == 0) return n; | ||
|
||
// If second string is empty, the only option is to | ||
// remove all characters of first string | ||
if (n == 0) return m; | ||
|
||
// If last characters of two strings are same, nothing | ||
// much to do. Get the count for | ||
// remaining strings. | ||
if (s1.charAt(m - 1) == s2.charAt(n - 1)) | ||
return editDistRec(s1, s2, m - 1, n - 1); | ||
|
||
// If last characters are not same, consider all three | ||
// operations on last character of first string, | ||
// recursively compute minimum cost for all three | ||
// operations and take minimum of three values. | ||
return 1 + Math.min(Math.min(editDistRec(s1, s2, m, n - 1), // Insert | ||
editDistRec(s1, s2, m - 1, n)), // Remove | ||
editDistRec(s1, s2, m - 1, n - 1)); // Replace | ||
} | ||
|
||
// Wrapper function to initiate the recursive calculation | ||
public static int editDist(String s1, String s2) { | ||
return editDistRec(s1, s2, s1.length(), s2.length()); | ||
} | ||
|
||
// Driver code | ||
public static void main(String[] args) { | ||
String s1 = "GEEXSFRGEEKKS"; | ||
String s2 = "GEEKSFORGEEKS"; | ||
System.out.println(editDist(s1, s2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters