In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.Return the sentence after the replacement. Example 1:Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"Output: "the cat was rat by the bat"Example 2:Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"Output: "a a b c"
Question
In English, we have a concept called root, which can be followed by some other word to form another longer word - let's call this word derivative. For example, when the root "help" is followed by the word "ful", we can form a derivative "helpful".Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the derivatives in the sentence with the root forming it. If a derivative can be replaced by more than one root, replace it with the root that has the shortest length.Return the sentence after the replacement. Example 1:Input: dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"Output: "the cat was rat by the bat"Example 2:Input: dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"Output: "a a b c"
Solution
This problem can be solved using a Trie data structure and a depth-first search algorithm. Here are the steps:
-
Initialize a Trie and add all the root words from the dictionary into the Trie.
-
Split the sentence into words.
-
For each word in the sentence, search for it in the Trie. If the word is found, replace it with the root word. If the word is not found, keep the word as it is.
-
Join all the words back into a sentence and return the sentence.
Here is a Python solution:
class TrieNode:
def __init__(self):
self.children = {}
self.endOfWord = False
class Solution:
def replaceWords(self, dictionary, sentence):
self.root = TrieNode()
for word in dictionary:
self.insert(word)
sentence = sentence.split()
for i in range(len(sentence)):
sentence[i] = self.search(sentence[i])
return ' '.join(sentence)
def insert(self, word):
Similar Questions
The Process of reducing a word such as a noun to a shorter version and using it as a new word such as a verb is called
A process that reduces the size of a vocabulary by reducing to the 'root' of words is called:Question 19Select one:a.Stemmingb.Lemmatizingc.Removal of stop wordsd.Postinge.pruningClear my choice
The Process of reducing a word such as a noun to a shorter version and using it as a new word such as a verb is called? ans. Backformation Clipping Blending Compounding
he Process of reducing a word such as a noun to a shorter version and using it as a new word such as a verb i
A grouping of letters added to the beginning of a word to add to or change the meaning of the root is known as arootsuffixstemprefix
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.