Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. void setup() {
  2.   println(editDist("apple", "bapple"));
  3. }
  4.  
  5. int editDist(String a, String b) {
  6.   // base cases
  7.   // note: getting string length: https://www.tutorialspoint.com/java/java_string_length.htm
  8.   if (a.length() == 0) {
  9.     return b.length();
  10.   } else if (b.length() == 0) {
  11.     return a.length();
  12.   } else {
  13.     // figuring out if last characters are equal
  14.     // note: getting character at specific position in string: https://www.tutorialspoint.com/java/java_string_charat.htm
  15.     int x = 0;
  16.     if (a.charAt(a.length() - 1) != b.charAt(b.length() - 1)) {
  17.       x = 1;
  18.     }
  19.     // note: getting a new string without last character: https://www.tutorialspoint.com/Java-String-substring-Method-example
  20.     return min(
  21.       editDist(a.substring(0, a.length() - 1), b) + 1,  // deletion
  22.       editDist(a, b.substring(0, b.length() - 1)) + 1,   // insertion
  23.       editDist(a.substring(0, a.length() - 1), b.substring(0, b.length() - 1)) + x  // substitution
  24.      );
  25.   }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement