Loading...
You are given roots, a list of lowercase strings, and sentence, a string of lowercase words separated by single spaces.
Replace every word of sentence that has some element of roots as a prefix with that root. When several roots are prefixes of the same word, use the shortest one. Words with no root as a prefix are left unchanged.
Return the resulting sentence, words separated by single spaces.
Input: roots = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
Explanation: "cattle" starts with "cat", "rattled" starts with "rat", and "battery" starts with "bat". "the", "was" and "by" have no root as a prefix.
Input: roots = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
Output: "a a b c"
Explanation: Every word starts with one of the single-letter roots, so each collapses to that letter.
roots.length ≤1000roots[i].length ≤100roots[i] consists of lowercase letterssentence.length ≤106sentence consists of lowercase letters and spacessentence is in [1,1000], each of length in [1,1000]Click "Run" to test with sample cases or "Submit" to run all tests.