Lexicographically minimal string made of two collections where take a letter from a collection only when it comes on the top of the stack which contains some criteria to get the top.
Inputs & Outputs 5 DAD DAD DADADD
ABCBA BCBA ABBCBACBA
BAC BAB BABABC
DAD DABC DABCDAD
YZYYZYZYY ZYYZYZYY YZYYZYYZYZYYZYZYY
ZZYYZZZA ZZYYZZZB ZZYYZZYYZZZAZZZB