xmen_wolv
BAN USER- 0of 0 votes
AnswersGiven a list of arraylists containing elements, write a function that prints out the permutations of of the elements such that, each of the permutation set contains only 1 element from each arraylist and there are no duplicates in the list of permutation sets.
- xmen_wolv in United States
Eg: consider the following lists
L1= {a1,b1,c1,d1}
L2={a2,b2,c2}
L3= {a3, b3, c3}
Valid Permutations are:
{a1,b2,c3}
{a1,a2,a3}
{b1,c2,b3}
...
...
..
Please note that
{a1,b2,c3} is same set as {b2,a1,c3}| Report Duplicate | Flag | PURGE
VMWare Inc Member Technical Staff Algorithm
public class CharNumberAI {
public static void Do()
{
Map<Integer, List<String>> listHashMap = new HashMap<Integer, List<String>>();
List<List<String>> inputList=new ArrayList<List<String>>();
List<String> arrayList=null;
arrayList=new ArrayList<String>();
arrayList.add("a1");
arrayList.add("b1");
inputList.add(arrayList);
arrayList=new ArrayList<String>();
arrayList.add("a2");
arrayList.add("b2");
inputList.add(arrayList);
arrayList=new ArrayList<String>();
arrayList.add("a3");
arrayList.add("b3");
inputList.add(arrayList);
PrintPowerSet(inputList, inputList.size()-1, new HashSet<String>());
}
private static void PrintPowerSet(List<List<String>> allchars, int index, Set<String> permSet)
{
List<String> stringList = allchars.get(index);
Set<String> subPremSet = null;
for(String str :stringList)
{
subPremSet=new HashSet<String>();
subPremSet.addAll(permSet);
if (index == 0)
{
subPremSet.add(str);
printSet(subPremSet);
}
else
{
//Console.Write(ch);
subPremSet.add(str);
PrintPowerSet(allchars, index - 1, subPremSet);
}
}
}
public static void printSet(Set<String> hashSet){
Iterator<String> i=hashSet.iterator();
System.out.print("{ ");
while(i.hasNext()){
System.out.print(i.next()+", ");
}
System.out.println("}");
}
}
Thanks!
- xmen_wolv October 17, 2013