Classic hash-map problem, bucket strings by their sorted form, then return the buckets. Hindi line-by-line commentary, code, and a discussion of when you should use frequency-tuple keys instead of sorted-string keys.
Neeche is topic ka Hinglish explanation hai, wahi jo hamare course lesson mein audio narration ke saath chalta hai. Code English mein hai, samjhana Hindi mein.
Group Anagrams woh problem hai jahan tum graduate karte ho "hash map mein value dhundhne" se "hash map ko equivalence class ka bucket banane" tak. Trick ek line ka code hai: har anagram family ke liye ek canonical key chuno. Key sahi pakdi to baaki khud likh jaata hai. Yeh problem India ke lagbhag har product company ke online assessment mein aata hai, usually Medium screening question ki tarah.
Problem statement: lowercase strings ka array diya hai, anagrams ko group karke koi bhi order mein groups return karo. Do words anagram hain agar ek doosre ke letters ka rearrangement ho. Input size das hazaar words tak ja sakta hai, aur har word sau characters tak, to words ki number mein quadratic kuch bhi time out ho jaayega.
Pehle pattern pehchano. Jab bhi problem bole "items group karo jo property P share karte hain", template answer hai: property P ke liye ek canonical representative chuno, phir hash map mein us representative ke key se items bucket karo. Anagrams ke liye canonical representative hai word ke sorted letters, ya equivalent count tuple. Yahi template Group Shifted Strings, Find Duplicate Files, aur Group People with Common Languages mein chalta hai.
Pehle obvious brute force se shuru karo taaki interviewer sune tum first principles se reason kar rahe ho. Har unprocessed word ke liye, har baad waale word ko scan karo aur Valid Anagram routine se check karo anagram hai kya. Pairwise check O of k hai jahan k word length hai. N words mein O of n squared k kaam karte ho. N equal to das hazaar ke liye yeh dus crore per-character compares: bahut slow.
Unlock. Do words anagram hain tab aur sirf tab jab unke sorted letters identical hain. To har pair compare karne ke bajaay, har word ka sorted-letter signature ek baar compute karo aur use hash-map key ki tarah use karo. Same signature waale words automatically same bucket mein gir jaate hain. Array ka ek pass, har word par almost constant kaam, koi pairwise compare nahi: O of n times k log k total.
Optimal Python solution aath lines ka hai. defaultdict of list use karo taaki if-key-exists check likhna na pade. Har word ke liye, key compute karo sorted letters ko join karke. Original word ko groups of key mein append karo. Dictionary ke values list of lists ki tarah return karo. Yeh hai pura algorithm.
Chaar edges test karne ke liye. Empty string input mein: empty ka sorted empty hai, to saare empty strings empty key ke neeche group ho jaate hain. Single word input mein: ek bucket size ek ka. Saare distinct non-anagram words: n buckets, har ek size ek. Duplicate words jaise "abc" do baar: same key share karte hain aur same bucket mein gir jaate hain kisi bhi anagram ke saath.
Brute force pairwise check O of n squared times k hai: n equal to das hazaar aur k equal to sau ke liye yeh ten to the eleven character compares, minutes ka CPU. Sorted-key hash map O of n times k log k hai: lagbhag saat million ops, instant. Count-tuple version log factor poori tarah drop kar deta hai for O of n times k: lagbhag dus lakh ops. Donon mein se koi bhi interview mein acceptable hai.
India context. Group Anagrams Flipkart, Amazon India, Microsoft India, aur Adobe India ka favourite hai SDE-1 aur SDE-2 phone screens ke liye. Razorpay aur PhonePe count-tuple variant specifically poochte hain kyunki yeh test karta hai tum samajhte ho ki log factor kyun hata sakte ho. TCS Digital aur Infosys SP jaise service company tests mein simpler "array mein word X ke saare anagrams find karo" aata hai: same canonical-key idea, bas end mein ek key lookup.
from collections import defaultdict
def group_anagrams_count(strs):
groups = defaultdict(list)
for s in strs:
counts = [0] * 26
for c in s:
counts[ord(c) - ord('a')] += 1
groups[tuple(counts)].append(s)
return list(groups.values())
Complexity: Brute force (pairwise compare): time O(n² · k log k), space O(n · k); Sorted-key hash map: time O(n · k log k), space O(n · k); Count-tuple hash map: time O(n · k), space O(n · k).
This free course is built for BTech / BE / BSc / MCA students preparing for campus placements at Amazon, Microsoft, Google, Adobe, Flipkart, Razorpay and similar Indian product companies; also professionals targeting switch interviews. If you have ever Googled group anagrams leetcode solution in hindi (hinglish) and bounced off paid-subscription paywalls or content that assumes you already know everything, this is the course for you.
This page is part of our flagship free course, Blind 75 LeetCode. The full course is delivered as short, focused lessons, each with bilingual narration (English and Hinglish), interactive practice widgets, and a clear before-and-after so you can feel yourself getting better. You can pause for weeks and pick up exactly where you left off, your progress is saved.
One phone-number signup unlocks every course on Techwave Academy: AI Bootcamp, Python for Class 4 / 6 / 8 / 10 / CBSE 12, Git, Linux, Spoken English, IOI Prep, Foundation Bootcamp, Blind 75, NeetCode 150, Claude Mastery, Tableau, Power BI, SQL, and Excel. Yes, all 18, forever free.
Blind 75 was assembled by an experienced interviewer specifically to cover every pattern that appears in SDE interviews, no duplicates, no fluff. It is the most efficient list for placement prep.
Both. Each lesson has a Hindi narration track for the explanation and English-only code with comments.
Every solution is provided in all three. Use the one your placement allows. If unsure, start with Python (cleanest), switch to C++ if you target high-frequency-trading or systems roles.
4-6 weeks if you do 2 problems a day with the in-browser editor and re-do tough ones.
Yes, 100% free, like every Techwave course.
Click below to open Blind 75 LeetCode. You will land on the course dashboard, see the week-by-week plan, and can start Lesson 1 immediately. No trial period, no payment screen, no upgrade pop-ups, ever.