0

I am trying to get all permutations of strings(words) in a java array list with two words.Following is the code i tried but i am not getting all permutations of words in the list.

for (int y = 0; y<newList.size(); y++){

      String first = newList.get(y);
      String second = "";

      if(y+1<newList.size()){
        second = newList.get(y+1);
      }

      ArrayList<String> tmpArr = new ArrayList<String>();
      tmpArr.add(first);
      tmpArr.add(second);

      ArrayList<String> retArray = combine(tmpArr);
      for (int c = 0; c <retArray.size(); c++) {
        System.out.println(retArray.get(c));
      }                 
}

public static ArrayList<String> combine(ArrayList<String> arr){
        ArrayList<String> retArr = new ArrayList<String>();
        if(arr.size()==0){
            retArr.add("");
            return retArr;
        }
        ArrayList<String> tmpArr = (ArrayList<String>)arr.clone();
        tmpArr.remove(0);

        for(String str1 : combine(tmpArr)){
            for(String str2 : arr){
                retArr.add(str2+","+str1);
                if(retArr.size() == 10)
                    return retArr;
            }

        }
        return retArr;
}

Please let me know how to correct the code to get all permutations of words in the list with size 2(all permutations of words with two words as output)

For example if the input data is as follows Input - [visit,party,delegation]

Expected Output - [visit,party visit,delegation party,visit party,delegation delegation,visit delegation,party]

Current Output - [visit,party, party,party, party,delegation, delegation,delegation,]

3
  • please post us the problems you are facing (errors, exceptions) and if not the current output what you are getting Commented May 21, 2014 at 9:43
  • The if statement in your for loop should be a loop from y+1 through the end of the list. I've posted simple code in an answer. Commented May 21, 2014 at 10:44
  • Whoops, misread your expected output. Modified my answer accordingly Commented May 21, 2014 at 10:55

2 Answers 2

1

This will give you all the two-word permutations of a list of words.

List<String> strings = Arrays.asList("party","visit","delegation");

for (int i = 0; i < strings.size(); i++){
  for (int j = 0; j < strings.size(); j++){
    if (i != j) System.out.println(strings.get(i) + "," + strings.get(j));
  }
}

EDIT: added the if statement and changed the loop since you wanted permutations of different orders as well.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you consider: delegation, delegation a permutation?
Based on OP's expected output, no.
0

You should step through your program with a debugger to see what happens.

My suggestion for the fault reason is: you create a new retArr in every call of combine, so you get only the results of the last call.

See that you use the same retArr for all executions of combine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.