Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 49 · Hashing / Anagram

Group Anagrams

LeetCode 49 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Key = sorted word (or letter-count tuple); group by key.

நடுத்தரம்ArrayHash TableStringSorting
LeetCode இல் திற ↗

நான் Group Anagrams (LeetCode 49) ஐ இப்படித் தீர்த்தேன் — Hashing / Anagram பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Anagrams share a signature — the same multiset of letters. Use that signature as a dictionary key and every word lands in its group automatically. The signature can be the sorted string or a 26-count tuple.

Approach

  1. 1

    For each word, compute its signature.

  2. 2

    Sorted letters: 'eat' → 'aet'.

  3. 3

    Count tuple: 26 counts, avoids sorting each word.

  4. 4

    Append the word to mpp[signature]; return the groups.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Sorted-string keyO(n·k log k) time

Group by ''.join(sorted(word)).

2
Count-tuple keyAcceptedO(n·k) time

Group by a 26-length frequency tuple.

Sorted-string keyPythonO(n·k log k) time
class Solution:
    def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
        mpp = defaultdict(list)
        for cur_string in strs:
           sorted_string= "".join(sorted(cur_string))
           mpp[sorted_string].append(cur_string)
        res =[]
        for key,val in mpp.items():
            res.append(val)
        return res
"""
maintain a freq arr of size 26 for each string
abbcccc
{(1,2,4,0.....)}
bbacccc
{(1,2,4,0.....)}
"""
Count-tuple keyPythonO(n·k) time
class Solution:
    def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
        mpp = defaultdict(list)
        res=[]
        for cur_string in strs:
            freq_arr = [0]*26
            for char in cur_string:
                freq_arr[ord(char)-ord('a')]+=1
            mpp[tuple(freq_arr)].append(cur_string)
        
        for key,val in mpp.items():
            res.append(val)
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Group Anagrams — அடிக்கடி கேட்கப்படும் கேள்விகள்

Group Anagrams பின்னாலுள்ள intuition என்ன?

Anagrams share a signature — the same multiset of letters. Use that signature as a dictionary key and every word lands in its group automatically. The signature can be the sorted string or a 26-count tuple.

Group Anagrams (LeetCode 49) ஐ எப்படித் தீர்ப்பது?

1. For each word, compute its signature. 2. Sorted letters: 'eat' → 'aet'. 3. Count tuple: 26 counts, avoids sorting each word. 4. Append the word to mpp[signature]; return the groups.

Group Anagrams இன் time மற்றும் space complexity என்ன?

Time: O(n · k) with counts (O(n · k log k) with sorting). Space: O(n · k). (Accepted approach: Count-tuple key.)

Group Anagrams இல் பொதுவான தவறுகள் என்ன?

Lists aren't hashable — convert the count array to a tuple.

Group Anagrams ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Sorted-string key (O(n·k log k) time): Group by ''.join(sorted(word)). Count-tuple key (O(n·k) time): Group by a 26-length frequency tuple.