![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
having trouble with permutations
hello, I am writing this progam to find all of the permuations of a string, but I am stuck. I can get it to give me strings as the output, but some of the strings are the same.
here is the code that I have already: public class StringPerm {
private String word;
private int wordLength;
private int numOfPerms;
private String[] permWords;
private int[] storeNums;
private int[] tmpArray;
private int[][] permNums;
private int count = 1;
public StringPerm(String word){
this.word = word;
wordLength = word.length();
numOfPerms = numberOfPermutations(wordLength);
permWords = new String[numOfPerms];
permNums = new int[numOfPerms][wordLength];
storeNums = new int[wordLength];
for(int i = 0; i < storeNums.length; i++){
storeNums[i] = i;
}
for(int k = 0; k < numOfPerms; k++){
permWords[k] = "";
}
permWords[0] = word;
}
public void makePermutations(){
ArrayUtils au = new ArrayUtils();
tmpArray = storeNums;
au.arrayShuffle(storeNums);
//for(int i = 0; i < numOfPerms; i++){
while(count < numOfPerms){
if(isNew(storeNums)){
au.arrayShuffle(storeNums);
append(storeNums);
count++;
}
}
printPermutations();
}
public void append(int[] array){
String tmpString = "";
//add the numbers into permNums for later testing
for(int i = 0; i < array.length; i++){
permNums[count][i] = array[i];
}
//take the number from the array passed in to be used to find the word
for(int k = 0; k < array.length; k++){
tmpString = tmpString + word.charAt(array[k]) + "";
}
//add the word to permWords
permWords[count] = tmpString;
}
/*
public boolean isNew(int[] array){
boolean isNew = true;
for(int i = 0; i < numOfPerms; i++){
for(int k = 0; k < wordLength; k++){
if(permNums[i][k] == array[k]){
return isNew = false;
}
}
}
return isNew;
}
*/
public boolean isNew(int[] array){
boolean isNew = false;
String tmpStr = "";
for(int i = 0; i < array.length; i++){
tmpStr = tmpStr + word.charAt(array[i]);
}
for(int k = 0; k < count; k++){
String tmpWord = permWords[k];
if(stringTest(tmpWord, tmpStr)){//getting cuaght here
return isNew = true;
}
}
return isNew;
}
public boolean stringTest(String a, String b){
int falseCount = 0;
for(int i = 0; i < wordLength; i++){
if(a.charAt(i) == b.charAt(i)){
falseCount++;
}
}
if(falseCount == wordLength)
return true;
return false;
}
public int numberOfPermutations(int length){
int total = length;
for (int i = length - 1; i > 0; i--){
total *= i;
}
return total;
}
public void printPermutations(){
for(int i = 0; i < numOfPerms; i++){
System.out.println(permWords[i]);
}
}
}ArrayUtils sortArray, just sorts the array randomly, in case anybody was wondering. Well any help would be great. Thanks! |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Here's a really simple way to permutate something:
public static void permu(String orig, String st)
{
if(st.length() == orig.length())
{
System.out.println(st);
}
else
{
for(int i = 0; i<orig.length(); i++)
{
if(st.indexOf(orig.charAt(i))==-1)
{
permu(orig, st+orig.charAt(i));
}
}
}
}The drawback to this is that it will only permutate wordw that don't have the same character in the word, however you could easily use a variation of this to find what you are looking for. EDIT: Sorry for the spacing, for some reason it coped and pasted like that. I'll fix it when I get home.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
yeah thanks for that code snippet. I actually found something written in c++ just using numbers, so I took that made ported it to java, and then converted the numbers to the corresponding letters when I printed the output. But yours and mine do pretty much the same thing.
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;
}
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|