Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void setup() {
- println(editDist("apple", "bapple"));
- }
- int editDist(String a, String b) {
- // base cases
- // note: getting string length: https://www.tutorialspoint.com/java/java_string_length.htm
- if (a.length() == 0) {
- return b.length();
- } else if (b.length() == 0) {
- return a.length();
- } else {
- // figuring out if last characters are equal
- // note: getting character at specific position in string: https://www.tutorialspoint.com/java/java_string_charat.htm
- int x = 0;
- if (a.charAt(a.length() - 1) != b.charAt(b.length() - 1)) {
- x = 1;
- }
- // note: getting a new string without last character: https://www.tutorialspoint.com/Java-String-substring-Method-example
- return min(
- editDist(a.substring(0, a.length() - 1), b) + 1, // deletion
- editDist(a, b.substring(0, b.length() - 1)) + 1, // insertion
- editDist(a.substring(0, a.length() - 1), b.substring(0, b.length() - 1)) + x // substitution
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement