There's a lot of guide on finding Permutation for a String, but how would I do this for every element in an ArrayList? Given that:
ArrayList<String> list = [bob, cat, dog]
Output:
[bob, cat, dog]
[bob, dog, cat]
[dog, bob, cat]
[dog, cab, bob]
....
Here's a code that I have that will permute a single word:
public class permutations {
public ArrayList<String> performPermutations(String s){
ArrayList<String> arrayList = new ArrayList<String>();
if (s == null) {
return null;
}
else if (s.length() == 0) {
arrayList.add("");
return arrayList;
}
else {
for (int i = 0; i < s.length(); i++) {
ArrayList<String> remaining = performPermutations(s.substring(0, i) + s.substring(i + 1));
for (int j = 0; j < remaining.size(); j++) {
arrayList.add(s.charAt(i) + remaining.get(j));
}
}
return arrayList;
}
}
public static void main(String[] args) {
permutations p = new permutations();
ArrayList<String> arr = p.performPermutations("abc");
for(int i = 0; i<arr.size();i++) {
System.out.println(arr.get(i));
}
}