| | |
permutation
![]() |
This might not be of help if this is homework.
http://www.google.co.uk/search?hl=en...ons+java&meta=
Why bother reinventing the wheel?
http://www.google.co.uk/search?hl=en...ons+java&meta=
Why bother reinventing the wheel?
*Voted best profile in the world*
•
•
Join Date: Apr 2004
Posts: 573
Reputation:
Solved Threads: 5
I actually found a better way to do this problem, less chaotic as the way I was doing it.
Here is the source for it:
Thanks for the ideas though.
Here is the source for it:
Java Syntax (Toggle Plain Text)
public class StringPerm { private int count = -1; private int numOfPerms; private String word; private int stringLength; private int[] permNums; private String[] permWords; private int permWordsCt = 0; public StringPerm(String word) { this.word = word; stringLength = word.length(); numOfPerms = numberOfPermutations(stringLength); permNums = new int[stringLength]; permWords = new String[numOfPerms]; } public void makePermutations(int characterPos) { count++; permNums[characterPos] = count; if (count == stringLength) { append(); } else { for (int i=0; i<stringLength; i++) { if (permNums[i] == 0) { makePermutations(i); } } } count--; permNums[characterPos] = 0; } public void append(){ String tmpWord = ""; for (int i = 0; i < stringLength; i++) { tmpWord = tmpWord + word.charAt(permNums[i] - 1); } if(permWordsCt < numOfPerms){ permWords[permWordsCt] = tmpWord; permWordsCt++; } } public void printPermutations(){ for(int i = 0; i < numOfPerms; i++){ System.out.println(permWords[i]); } } public int numberOfPermutations(int length){ int total = length; for (int i = length - 1; i > 0; i--){ total *= i; } return total; } }
![]() |
Similar Threads
- permutation of a string in c++ (C++)
- Permutation (C++)
- Prolog permutation (Legacy and Other Languages)
Other Threads in the Java Forum
- Previous Thread: Gui TextFeild to an int?? Help!!
- Next Thread: I waWNT HELP ON JAVA PROJECT TELL ME JAVA PROJECTS
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






