Hey guys can you help me convert this code into java... i started a little...

def  minEditDistR(target, source):
   """ Minimum edit distance. Straight from the recurrence. """

   i = len(target); j = len(source)

   if i == 0:  return j
   elif j == 0: return i

   return(min(minEditDistR(target[:i-1],source)+1,
              minEditDistR(target, source[:j-1])+1,
              minEditDistR(target[:i-1], source[:j-1])+substCost(source[j-1], target[i-1])))

def substCost(x,y):
    if x == y: return 0
    else: return 2

Here is me trying

public static int distance (String s1, String s2){
		int a = s1.length(); 
		int b = s2.length();
		
		if ( a == 0) return b;
		if ( b == 0) return a;

Thank YOU!

You're almost there, you just need a function that mimics min() (does the Java standard library have one?) and substCost().

Looks like there is one, but it only take two arguments.

Here is what I have so far:

public static int distance(String s1, String s2) {

    int a = s1.length(); 
    int b = s2.length();
		
    if(a == 0)
        return b;
    if(b == 0)
        return a;

    return Math.min(Math.min(minEditDistR(target.substring(0, i - 1), source) + 1,
                             minEditDistR(target, source.substring(0, j - 1)) + 1)),
                             minEditDistR(target.substring(0, i - 1), source.substring(0, j - 1)) +
                                 substCost(source.charAt(j - 1), target.charAt(i - 1)));
}

public int substCost(x, y) {
    if(x == y)
        return 0;
    else
        return 2;
}

Just tell me if anything is wrong with it.

Edited 5 Years Ago by DecreaseEntropy: n/a

This article has been dead for over six months. Start a new discussion instead.