roots and a text text whose words are separated by single spaces.
Whenever a word of the text starts with a root from the dictionary, replace the whole word with that root. If several roots fit, use the shortest one. Words that start with no root stay unchanged.
Return the resulting text, keeping the word order and the single spaces between words.roots=['sun', 'moon', 'star'], text='sunflowers bloom under starlight and moonbeams' → 'sun bloom under star and moon'
"sunflowers" starts with the root sun, "starlight" with star and "moonbeams" with moon. The other words start with no root and stay as they are.
roots=['pre', 'p', 'pref'], text='prefix press apple' → 'p p apple'
"prefix" and "press" start with several roots at once, so the shortest one, p, wins.
roots=['rain'], text='rain rainbow train' → 'rain rain train'
The root must sit at the very start of the word: "train" contains rain but does not begin with it.
len(roots) ≤ 10⁴len(roots[i]) ≤ 100len(text) ≤ 10⁶roots=['sun', 'moon', 'star'], text='sunflowers bloom under starlight and moonbeams'
'sun bloom under star and moon'
Run checks the first 3 tests in your browser. Submit checks all 185 tests on the server, hidden ones included.