Skip to main content

Posts

Showing posts from June, 2024

648. Replace Words

Today's post brings out the awesome question from "problem of the day" on Leetcode. This code implements a solution to replace words in a sentence with their corresponding root words based on a given dictionary of root words. Let's break down the code step by step: Intuition - So we were provided with the list of words in dictionary and a sentence. For each word in sentence we need to find if that word is starting from any word in the given dictionary. If yes then we need to return that word from dictionary in-place of original word in sentence.  So it can be either processed using nested loops. one loop on sentence words and inside loop over dictionary words and find out which word in dictionary in the root in word in sentence. For small inputs we can do it but who we are kidding in real life scenario we operate on thousands and sometimes millions of data entries. So check the code constraints and you will realise what i am saying. Approach- We will optimise the sea