Skip to content

648. Replace Words #168

Jul 30, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We can approach this using a Trie (prefix tree) to store the dictionary of roots. Then, we process each word in the sentence, replacing it with the shortest root if it exists in the Trie.

Approach:

  1. Trie Structure:

    • We'll build a Trie (prefix tree) from the dictionary of roots. This allows us to efficiently find the shortest root for each word in the sentence.
  2. Processing the Sentence:

    • For each word in the sentence, we'll search the Trie to see if there's a root that matches the start of the word. Once we find the first matching root, we replace the word with that root.
  3. Efficiency:

    • By using a Trie, searching for the root for each word will be faster than directly comparing the wor…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 10, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants