You do not need to search for all combinations for this a.
Suppose that ais “peace” and b“hello”. You can find the largest character in awhich is less than or equal to the first character b. In this case, you will find "d" in a. This is the biggest character you can start with your permutations a, so you should start with it. Since the "d" is strictly smaller than the "h", you can simply rank all the remaining characters afrom largest to smallest, and you will get "dwrol".
, , a - "", b - "", a b. , a, 'l'. "w", "o", "r", "d" . , "like", "d", "ldwro".
Edit:
( ) . a , b, a , b (, 0 a). , . a , b, a, a b. , , .
a 0s, , 0 , b, .