Statements after Recursion Call in Java

Viewed 56

I'm trying to learn recursion but am having a hard time understanding the following code.Specifically, When these two statements [remainLetters = insertAtIndex(remainLetters, tmpString, i);scramLetters = removeFromIndex(scramLetters, scramLetters.length() - 1) ]execute for the first time, remainLetters is set to be "d", making the remainLetters length of 1 while i = 0, but why didn't it enter the for loop afterwards since it satisfies the condition: i < remainLetters.length()? Thank you in advance!

public class WordScrambler {
/* Output every possible combination of a word.
   Each recursive call moves a letter from
   remainLetters" to scramLetters".
*/
public static void scrambleLetters(String remainLetters,  // Remaining letters
                                   String scramLetters) { // Scrambled letters
    String tmpString;      // Temp word combinations
    int i;                 // Loop index

    if (remainLetters.length() == 0) { // Base case: All letters used
        System.out.println(scramLetters);
    }
    else {                             // Recursive case: move a letter from
        // remaining to scrambled letters
        for (i = 0; i < remainLetters.length(); ++i) {
            // Move letter to scrambled letters

            tmpString = remainLetters.substring(i, i + 1);
            remainLetters = removeFromIndex(remainLetters, i);
            scramLetters = scramLetters + tmpString;

            scrambleLetters(remainLetters, scramLetters);

            // Put letter back in remaining letters

            remainLetters = insertAtIndex(remainLetters, tmpString, i);
            scramLetters = removeFromIndex(scramLetters, scramLetters.length() - 1);
        }
    }
}

// Returns a new String without the character at location remLoc
public static String removeFromIndex(String origStr, int remLoc) {
    String finalStr;      // Temp string to extract char

    finalStr = origStr.substring(0, remLoc);                     // Copy before location remLoc
    finalStr += origStr.substring(remLoc + 1, origStr.length()); // Copy after location remLoc

    return finalStr;
}

// Returns a new String with the character specified by insertStr
// inserted at location addLoc
public static String insertAtIndex(String origStr, String insertStr, int addLoc) {
    String finalStr;      // Temp string to extract char

    finalStr = origStr.substring(0, addLoc);                 // Copy before location addLoc
    finalStr += insertStr;                                   // Copy character to location addLoc
    finalStr += origStr.substring(addLoc, origStr.length()); // Copy after location addLoc

    return finalStr;
}

public static void main(String[] args) {
    
    scrambleLetters("word", "");
}

}

1 Answers

You would be correct about the state of your variables at the end of the first loop to finish (this is NOT the same as your first loop to begin due to recursion), but keep in mind your iteration statement increments i after every loop.

Inside the loop, the length of remainingLetters is decreased by one before making another recursive call. This continues until the length of the String is 0, in which case the if statement in the function evaluates as true and ignores the for loop (effectively ending that chain of recursion and advancing the outer loop).

Related