0

I have a Java program that calculates the Levenshtein distance between two strings. I use this method to do it:

    public static int levDistance(String s, int len_s, String t, int len_t) {
        if (len_s == 0)
            return len_t;
        if (len_t == 0)
            return len_s;

        int cost = 0;
        if (s.charAt(len_s - 1) == t.charAt(len_t - 1))
            cost = 0;
        else
            cost = 1;
        return Math.min(Math.min(
                levDistance(s, len_s - 1, t, len_t) + 1,
                levDistance(s, len_s, t, len_t - 1) + 1),
                levDistance(s, len_s - 1, t, len_t - 1) + cost);
}

As the title suggests, I need to get every step in this procedure. I don't really care how it's returned (could be newline-separated, in an arraylist, etc.) or in what order it's in, only that one character is changed at each step.

Examples:

distance("saturday", "sauterdai"); -> "saturday", "sauterday", "sauterdai"
distance("algorithm", "algurthm") -> "algorithm", "algorthm", "algurithm"
distance("stackoverflow", "mathoverflow") -> "stackoverflow", "tackoverflow", "tckoverflow", "tkoverflow", "toverflow", "mtoverflow", "matoverflow", "mathoverflow"
Loovjo
  • 534
  • 8
  • 23
  • The request for Java rather than pseudo-code, combined with the lack of Java code in the question, makes this a request for a code-writing service, rather than a question. – Patricia Shanahan May 30 '15 at 14:32
  • Please provide your code in `Java` and not in `pseudo-code` – Chetan Kinger May 30 '15 at 14:33
  • @PatriciaShanahan Fine, pseudo-code is allowed. – Loovjo May 30 '15 at 14:34
  • You still need code **in your question** not just a link. Links can go away, and many people do not follow links on general principles. – Patricia Shanahan May 30 '15 at 14:35
  • Don't do it recursively, use dynamic programming. From the table you create with dp, you can backtrack and find each step that took place. – vandale May 30 '15 at 14:45
  • Dynamic programming would definitely be better for this, because subproblems will re-occur during the search. With DP, the subproblem results can be retrieved without redoing the calculation. – Patricia Shanahan May 30 '15 at 19:21

1 Answers1

1

You cannot be sure a given change will be in the actual path until the recursion completes. Here is one strategy for dealing with that:

Define a data structure that captures a List<String> along with the integer cost of using that list as the chain of changes. Make levDistance return a reference to an instance of that data structure.

In the base case the list contains only the original two strings. For the non-base case, spread out the calculation of the minimum, and fill in the new intermediate string for the selected case.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75