nathanwailes

LeetCode 49 - Group Anagrams - NeetCode solution

Sep 30th, 2023
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.38 KB | None | 0 0
  1. class Solution:
  2.     def group Anagrams(self, strs: List[str]) -> List[List[str]]:
  3.         res = defaultdict(list) # mapping charCount to list of Anagrams
  4.         for s in strs:
  5.             count = [0] * 26 # a ... z
  6.            
  7.             for c in s:
  8.                 count[ord(c) - ord("a")] += 1
  9.            
  10.             res[tuple(count)].append(s)
  11.  
  12.         return res.values()
Advertisement
Add Comment
Please, Sign In to add comment