Skip to content

Latest commit

 

History

History
62 lines (45 loc) · 1.48 KB

_49. Group Anagrams.md

File metadata and controls

62 lines (45 loc) · 1.48 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 13, 2024

Last updated : July 01, 2024


Related Topics : Array, Hash Table, String, Sorting

Acceptance Rate : 69.41 %


Solutions

Python

class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        output = {}

        for s in strs :
            key = ''.join(sorted(s))

            if key in output :
                output[key].append(s)
            else :
                output[key] = [s]

        return [x for x in output.values()]
class Solution:
    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
        output = {}

        for s in strs :
            cnt = [0] * 26
            for c in s :
                cnt[ord(c) - ord('a')] += 1
            cnt = tuple(cnt)
            
            if cnt in output :
                output[cnt].append(s)
            else :
                output[cnt] = [s]

        return [x for x in output.values()]