Replace Words with Roots (Stemming)

MEDIUM

You are given a dictionary of "roots" (e.g., a list or set of strings like {"cat", "bat", "rat"}) and a "sentence" (a string of words, e.g., "the cattle was rattled by the battery"). Write a Python function to replace each word in the sentence with its corresponding root if the word is a "successor" of a root (i.e., the word starts with a root).

If a word can be formed by multiple roots, it should be replaced by the shortest root.

If a word is not a successor of any root, it should remain unchanged.

Examples:

Example 1:

Input: roots = ["cat", "bat", "rat"], sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"

Example 2:

Input: roots = ["a", "b", "c"], sentence = "apple banana carrot"
Output: "a b c"

Example 3 (Shortest Root):

Input: roots = ["sub", "substitute", "replace"], sentence = "substitute all replacements"
Output: "sub all replace" ( "substitute" is replaced by "sub" because it's shorter)

Example 4 (No Replacement):

Input: roots = ["xyz"], sentence = "a quick brown fox"
Output: "a quick brown fox"

Constraints:

  • The input roots will be a list of strings.
  • The input sentence will be a string.
  • Roots and words consist of lowercase English letters.

Function Signature (Python):

from typing import List, Set # Set is good for roots for O(1) avg lookup if needed, but list is fine too

class Solution:
    def replace_with_roots(self, roots: List[str], sentence: str) -> str:
        # Your code here
        pass

 

Nerchuko Academy · Free DS Interview Prep