I've a string, which is as follows,
x y z
and a corresponding HashMap as follows,
{
x: [1,2,5,6,7],
y: [3,4],
z: [3,8,9,10]
}
I need to generate all possible combinations of the string x y z
. So output will be as follows,
1 3 3
1 3 8
1 3 9
... so on
Note that, number of characters can be different and the keys in the hash will be same as number of variables in the string. Another example could be as follows,
x y
Corresponding HashMap as follows,
{
x: [3,5],
y: [4]
}
I've tried coding using 2 for-loops, but looks like that is not the right way to go about. I've tried, something as follows,
List<String> permutations = new List<>();
for(int i = 0; i < Str.length; i++) {
for (Map.Entry<String, List<Integer>> entry : map.entrySet()) {
for(Integer val : entry. getValue()){
//substitute
}
permutations.add(<substitured-string>);
}
}
I couldn't think of writing a code which hash dynamic number of for loops. How do I solve this problem of dynamic for loops to iterate?