Java: Saving the value of a recursive local counter

Viewed 202

I've created a method that counts the number of all matching digits from two given integers. Everything in the method seems to work as it should except the local counter.

The counter does "work" at counting the number of matching digits all the way to the final recursive iteration. However, as the recursion works it's way back out, the final (and desired) value is lost as all the previous values are cycled through until it reaches the original value. This means that no matter what value the counter got to during all the iterations, it will still always return 0.

How can I save and return the final value of the counter? Any help would be much appreciated, thanks.

public static int match(int a, int b) {
    return matchHelper(a, b, 0);
}

private static int matchHelper(int a, int b, int c) {
    int count = c;
    String strA = Integer.toString(a);
    String strB = Integer.toString(b);

    if (a < 0 || b < 0) {
        throw new IllegalArgumentException();
    } else {
        // Check and count
        if (strA.charAt(strA.length() - 1) == strB.charAt(strB.length() - 1)) {
            count++;
        }
        // Remove last char and call again
        if (strA.length() > 1 && strB.length() > 1) {
            strA = strA.substring(0, strA.length() - 1);
            strB = strB.substring(0, strB.length() - 1);
            matchHelper(Integer.parseInt(strA), Integer.parseInt(strB), count);
        }
    }
    return count;
}

NOTE: This method has many requirements and restrictions that has caused it to be coded this way (no loops, no structured objects, must be recursion, ect..). I'm sure there is a better way of doing this. However, my primary concern pertains to returning the correct value of the counter. Thanks.

2 Answers

How can I save and return the final value of the counter?

Maybe you should rephrase that as "How can I save the returned value of the counter?", to which the answer is: Use the return value.

count = matchHelper(...);

That fixes the problem.


You don't actually need the c parameter, or the helper method, if you use += instead:

public static int match(int a, int b) {
    int count = 0;
    String strA = Integer.toString(a);
    String strB = Integer.toString(b);

    if (a < 0 || b < 0) {
        throw new IllegalArgumentException();
    } else {
        // Check and count
        if (strA.charAt(strA.length() - 1) == strB.charAt(strB.length() - 1)) {
            count++;
        }
        // Remove last char and call again
        if (strA.length() > 1 && strB.length() > 1) {
            strA = strA.substring(0, strA.length() - 1);
            strB = strB.substring(0, strB.length() - 1);
            count += match(Integer.parseInt(strA), Integer.parseInt(strB));
        }
    }
    return count;
}

Your code is really going the very slow way, converting the numbers to strings just to extract the last digit. Don't do that, use division and remainder.

public static int match(int a, int b) {
    if (a < 0 || b < 0)
        throw new IllegalArgumentException();
    int count = 0;
    if (a % 10 == b % 10) // compare last digit
        count++;
    if (a >= 10 && b >= 10)
        count += match(a / 10, b / 10); // recurse with last digit removed
    return count;
}

If you insist on using strings, only convert to string once at the beginning, then "iterate" backwards comparing digits.

public static int match(int a, int b) {
    if (a < 0 || b < 0)
        throw new IllegalArgumentException();
    String strA = Integer.toString(a);
    String strB = Integer.toString(b);
    return matchHelper(strA, strB, strA.length() - 1, strB.length() - 1);
}
private static int matchHelper(String strA, String strB, int aIdx, int bIdx) {
    int count = 0;
    if (strA.charAt(aIdx) == strB.charAt(bIdx))
        count++;
    if (aIdx > 0 && bIdx > 0)
        count += matchHelper(strA, strB, aIdx - 1, bIdx - 1);
    return count;
}

All 4 solutions shown in this answer produces the same result (3) when tested with match(1236456789, 51782), since digits 5, 7, and 8 are matching.

i think you should assign the value to count after recursive call to matchHelper, consider the following code

public static int match(int a, int b) {
return matchHelper(a, b, 0);
}

private static int matchHelper(int a, int b, int c) {
int count = c;
String strA = Integer.toString(a);
String strB = Integer.toString(b);

if (a < 0 || b < 0) {
    throw new IllegalArgumentException();
} else {
    // Check and count
    if (strA.charAt(strA.length() - 1) == strB.charAt(strB.length() - 1)) {
        count++;
    }
    // Remove last char and call again
    if (strA.length() > 1 && strB.length() > 1) {
        strA = strA.substring(0, strA.length() - 1);
        strB = strB.substring(0, strB.length() - 1);
        count=matchHelper(Integer.parseInt(strA), Integer.parseInt(strB), count);
    }
}
return count;
}
Related