Вот неоптимизированная версия. Вероятно, есть более короткая версия с картой или лямбда, но ...
def shuffle(S):
words = S.split(' ')
letters = sorted([c for c in S if c != ' '])
new_words = []
i = 0
for l in letters:
placed = False
while not placed:
if len(new_words) < len(words):
new_words.append(l)
placed = True
else:
if len(new_words[i]) < len(words[i]):
new_words[i] += l
placed = True
i += 1
if i == len(words):
i = 0
return ' '.join(new_words)