Say I've got two partitions of a list (not necessarily containing numbers, and not necessarily distinct), and I want to check that one is a subpartition of the other. Let's look at some examples:
list = {1, 2, 3, 4, 5, 6};
p1 = {{1, 2, 3}, {4, 5, 6}};
p2 = {{1}, {2, 3}, {4, 5}, {6}};
p3 = {{1, 2}, {3, 4}, {5, 6}};
subpartitionQ[p1, p2] (* True *)
subpartitionQ[p1, p3] (* False *)
The second is a subpartition of the first in that the first can be formed by joining some consecutive subsets of the second back together. Note that this is not possible with p3
: this one contains 3
and 4
in the same sublist, whereas p1
doesn't, so I can't recover p1
merely by joining consecutive sublists.
Note that I also can't check whether all lists of pn
are subsets of some sublist of p1
, because the elements aren't necessarily distinct. E.g. list
could also have been {1, 1, 1, 1, 1, 1}
. This is only about the structure of the partitions.
Of course I could get all partitions of pn
, join each segment together and check if one of them is equal to p1
but that seems horribly inefficient and inelegant. Are there better ways to solve this?