pattern matching – Partition a string into the longest substrings of given characters

I want to partition the string into the longest substrings, each containing only specific characters, starting from left to right, without overlapping, always choosing the longest possible at the current position. In my example, only strings containing only characters d, f, g or d, e, h or a, b, c, g are allowed.





{"ABC", "D", "E", "FG", "H"}

But after "ABC" there is obviously a substring "OF" it's longer than "RE" or "E".
So, my expected exit would be:{ABC, DE, FG, H}

If I pass the first and second arguments of Alternatives this way:


then the output is as expected:

{"ABC", "DE", "FG", "H"}

But Alternatives should be from the definition something that is independent about the order of the arguments. I would expect both inputs to have the same output (the second one).

So my question is how to get the longest substring possible, regardless of the order of the arguments inside. Alternatives is?