Skip to main content

Posts

1963. Minimum Number of Swaps to Make the String Balanced

Recent posts

Group Anagrams – Python Solution Explained

Problem Overview The Group Anagrams problem is a common challenge in coding interviews and competitive programming. The goal is to group a list of strings such that all anagrams are placed in the same group. Anagrams are words that contain the same characters but in different orders. For example, given the input ["eat", "tea", "tan", "ate", "nat", "bat"] , the expected output is [['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']] . Approach and Solution In this blog post, we'll discuss an efficient solution using Python's built-in data structures. The provided code uses a dictionary to group the anagrams. Here’s a step-by-step explanation of the approach: Code Breakdown Explanation Initialization: We use defaultdict from the collections module to create a dictionary ( hm ) where each key will map to a list of anagrams. This is useful because it automatically initializes

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