943,945 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 7062
  • Java RSS
Mar 29th, 2006
0

permutation

Expand Post »
Hi, i was writing a program to make permutations of strings, but I ran into a snag and can't figure out what's wrong. It prints out the permutated strings, but some of them are duplicates.
I attached the source
Thanks for the help.
Attached Files
File Type: java StringPermDriver.java (288 Bytes, 104 views)
File Type: java ArrayUtils.java (335 Bytes, 76 views)
File Type: java StringPerm.java (1.8 KB, 150 views)
Similar Threads
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004
Mar 30th, 2006
0

Re: 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?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 30th, 2006
0

Re: permutation

Since you are using arraylist, use the contains() method to check if the entry already exists. If it doesn't, then add it.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Apr 9th, 2006
0

Re: permutation

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:
Java Syntax (Toggle Plain Text)
  1.  
  2. public class StringPerm {
  3. private int count = -1;
  4. private int numOfPerms;
  5. private String word;
  6. private int stringLength;
  7. private int[] permNums;
  8. private String[] permWords;
  9. private int permWordsCt = 0;
  10.  
  11.  
  12. public StringPerm(String word) {
  13. this.word = word;
  14. stringLength = word.length();
  15. numOfPerms = numberOfPermutations(stringLength);
  16. permNums = new int[stringLength];
  17. permWords = new String[numOfPerms];
  18. }
  19.  
  20. public void makePermutations(int characterPos) {
  21. count++;
  22. permNums[characterPos] = count;
  23.  
  24. if (count == stringLength) {
  25. append();
  26. }
  27. else {
  28. for (int i=0; i<stringLength; i++) {
  29. if (permNums[i] == 0) {
  30. makePermutations(i);
  31. }
  32. }
  33. }
  34. count--;
  35. permNums[characterPos] = 0;
  36. }
  37.  
  38. public void append(){
  39. String tmpWord = "";
  40. for (int i = 0; i < stringLength; i++) {
  41. tmpWord = tmpWord + word.charAt(permNums[i] - 1);
  42. }
  43. if(permWordsCt < numOfPerms){
  44. permWords[permWordsCt] = tmpWord;
  45. permWordsCt++;
  46. }
  47. }
  48.  
  49. public void printPermutations(){
  50. for(int i = 0; i < numOfPerms; i++){
  51. System.out.println(permWords[i]);
  52. }
  53. }
  54.  
  55. public int numberOfPermutations(int length){
  56. int total = length;
  57. for (int i = length - 1; i > 0; i--){
  58. total *= i;
  59. }
  60. return total;
  61. }
  62. }
Thanks for the ideas though.
Reputation Points: 23
Solved Threads: 6
Posting Pro
Dark_Omen is offline Offline
573 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Gui TextFeild to an int?? Help!!
Next Thread in Java Forum Timeline: I waWNT HELP ON JAVA PROJECT TELL ME JAVA PROJECTS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC