Question: Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains

Consider the permutations method from the textbook, which is intended to return all permutations of the word passed in as a parameter. Which line contains the terminating condition in the permutations recursive method?
public static ArrayList permutations(String word)
{
ArrayList result = new ArrayList();
if (word.length()==0)// line #1
{
result.add(word); // line #2
return result; // line #3
}
else
{
for (int i =0; i < word.length(); i++)// line #4
{
String shorter = word.substring(0, i)+ word(substring(i +1); // line #5
ArrayList shorterPermutations = permutations(shorter); // line #6
for (String s : shorterPermutations)// line #7
{
result.add(word.charAt(i)+ s); // line #8
}
}
return result; // line #9
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!