logo CodeStepByStep logo

EditDistance

Language/Type: VB recursion string return

Write a recursive function named EditDistance that accepts string parameters s1 and s2 and returns the "edit distance" between the two strings as an integer. Edit distance (also called Levenshtein distance) is defined as the minimum number of "changes" required to get from s1 to s2 or vice versa. A "change" can be defined as a) inserting a character, b) deleting a character, or c) changing a character to a different character.

Call Value Returned
EditDistance("driving", "diving") 1
EditDistance("debate", "irate") 3
EditDistance("football", "cookies") 6

Constraints: Your solution must obey the following constraints:

  • Your solution must not use any loops it must be recursive.
  • Strings have a member function named IndexOf, but you should not call it, because they allow you to get around using recursion. Similarly, the Replace member is forbidden. You should limit yourself to using only the following string members:
    • Chars, Length, Substring, Trim
  • Do not construct any data structures (no list, dictionary, etc.), and do not declare any global variables. You are allowed to define other "helper" functions if you like.
Function: Write a VB function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.