Finish generalized implementation. Generate prefix/suffix pairs and explore tree-recursively,
combining results at the end.
Use flat_map for efficiency.