permutation

Reply

Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

permutation

 
0
  #1
Mar 29th, 2006
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, 16 views)
File Type: java ArrayUtils.java (335 Bytes, 15 views)
File Type: java StringPerm.java (1.8 KB, 23 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: permutation

 
0
  #2
Mar 30th, 2006
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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: permutation

 
0
  #3
Mar 30th, 2006
Since you are using arraylist, use the contains() method to check if the entry already exists. If it doesn't, then add it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 573
Reputation: Dark_Omen is an unknown quantity at this point 
Solved Threads: 5
Dark_Omen Dark_Omen is offline Offline
Posting Pro

Re: permutation

 
0
  #4
Apr 9th, 2006
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:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC