Design a recursive program that generates all the permutations of a string. Impl
ID: 3623283 • Letter: D
Question
Design a recursive program that generates all the permutations of a string.Implement a class named SumPuzzle that includes a null constructor (a constructor definition that takes no arguments). The SumPuzzle class should also contain an instance method named permute(String str, LinkedList lst). The permute method pulls characters from lst in order to grow permutation strings inside str and it uses both iteration and recursion. Your base case should test for an empty lst argument, in which case you can store the str argument in a SumPuzzle attribute that collects all the permutations for you. You don’t need to know how many permutations you’ll be collecting if you use a linked list to collect them all.
The permute method doesn’t have to be a public method. In fact, it should probably be private. The permute method just a workhorse to be used by a public method named allPermutations(string). A call to allPermutations(string) should return a LinkedList containing all possible permutations of the argument string.
Be careful when you test your code – do not try it on strings that are very long. The number of permutations for a string of length n is factorial(n).
If it helps---
PascalSpecs.java http://twiki-edlab.cs.umass.edu/pub/CS187Spring2011/HW2Page/PascalSpecs.java
PuzzleSpecs.java http://twiki-edlab.cs.umass.edu/pub/CS187Spring2011/HW2Page/PuzzleSpecs.java
Explanation / Answer
import java.util.Iterator; import java.util.LinkedList; public class SumPuzzle { public SumPuzzle() { } public LinkedList allPermutations(String str){ LinkedList llist = new LinkedList(); for(int i=0;i 2) { LinkedList computedPerms = permute(str, copy); for (String item : computedPerms) { // the first letter + this computed permutation thisSol.add(removed + item); } } else { thisSol.add(removed + copy.getFirst() + copy.getLast()); thisSol.add(removed + copy.getLast() + copy.getFirst()); } } return thisSol; } } Please RATE!!!!Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.