I need help with creating an algorithm in PHP that, given an array of alphabets (represented as strings) and an array of groupings of those alphabets (also an array of strings), returns an array of arrays of all possible combinations of strings based on those groupings. The following example will make it clear -
If the input array is ['A', 'B', 'C']
and the groupings are ['AB', 'BC']
the returned output:
- Without any restrictions would be
[['A','B','C'], ['AB,'C'], ['A','BC'], ['AC','B'], ['ABC']]
- With the restrictions of the groupings should be
[['A','B','C'], ['AB,'C'], ['A','BC']]
The reason for this is because neither 'ABC' nor 'AC' are allowed groupings and the idea is that the groupings should only exist if they belong to the specified array. In this case, since 'AB' and 'BC' are the only possible groupings, the output contains them. The first output was just for demonstration purposes, but the algorithm should produce the second output. The only other restriction is that there can't be duplicate alphabets in a single combination. So the following output is NOT correct:
[['A','B','C'], ['AB,'C'], ['A','BC'], ['AB','BC'], ['AC','B'], ['ABC']]
since 'B' is a duplicate in ['AB','BC']
A similar question I found was here, except that there are no restrictions on which numbers can be grouped together in the "Result" in this question.
I apologize if I made it sound confusing but I'll be sure to clarify if you have any questions.