Java Reference
In-Depth Information
37
38// Generate all permutations of the simpler word
39 PermutationGenerator
shorterPermutationGenerator
40 = new
PermutationGenerator(shorterWord);
41 ArrayList<String>
shorterWordPermutations
42 =
shorterPermutationGenerator.getPermutations();
43
44// Add the removed character to the front of
45// each permutation of the simpler word
46 for (String s :
shorterWordPermutations)
47 {
48 result.add(word.charAt(i) +
s);
49 }
50 }
51// Return all permutations
52 return result;
53 }
54
55 private String word;
56 }
Compare the PermutationGenerator and Triangle classes. Both of them
work on the same principle. When they work on a more complex input, they first
solve the problem for a simpler input. Then they combine the result for the simpler
input with additional work to deliver the results for the more complex input. There
really is no particular complexity behind that process as long as you think about the
solution on that level only. However, behind the scenes, the simpler input creates
even simpler input, which creates yet another simplification, and so on, until one
input is so simple that the result can be obtained without further help. It is interesting
to think about this process, but it can also be confusing. What's important is that you
can focus on the one level that mattersȌputting a solution together from the slightly
simpler problem, ignoring the fact that it also uses recursion to get its results.
Search WWH ::




Custom Search