Loading
Loading
A note-taking app compresses text by replacing each word with a registered root — a short stem the word begins with. When several roots fit a word, the shortest one is used. Words with no matching root are left alone.
Given roots and a sentence of lowercase words separated by single spaces, return the sentence with every word replaced by its shortest root.
roots = ["re", "pre", "un"], sentence = "unwind before rerun prepay"unwindu → n hits the end of root unbeforeno root starts with b — keep the wordrerunr → e hits the end of reprepayp → r → e hits the end of pre"un before re pre"Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(total characters) time · O(total root characters) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
unwind → un, before keeps its form, rerun → re, prepay → pre.
Example 2
Both roots are prefixes of abc; the shorter one, a, wins. bca has no root.