Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Replace Words

Medium
triestringhash-table

Given a sentence and a dictionary of roots, replace every word in the sentence that has a root as a prefix with its shortest such root. Words without a matching root stay unchanged.

Input

  • Line 1: the sentence (words separated by single spaces)
  • Line 2: space-separated dictionary roots

Output: the transformed sentence.

Example 1
Input:
the cattle was rattled by the battery
cat bat rat
Output:
the cat was rat by the bat
Example 2
Input:
a aadsfasf absbs bbab cadsfafs
a b c
Output:
a a a b c

Constraints

  • 1 ≤ sentence words ≤ 1000
  • 1 ≤ roots ≤ 1000; lowercase letters.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.