Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 11, 2022 11:21
Show Gist options
  • Save codecakes/6b67cc086426c5201d9f79f454afe3e5 to your computer and use it in GitHub Desktop.
Save codecakes/6b67cc086426c5201d9f79f454afe3e5 to your computer and use it in GitHub Desktop.
from typing import Optional, List
from collections import defaultdict
from itertools import groupby
def grouped_anagram(anagram_arr: List[str]) -> List[List[str]]:
w_group = defaultdict(list)
for key_sum, word in groupby(anagram_arr, lambda w: sum(map(ord, w))):
w_group[key_sum] += list(word)
return list(w_group.values())
print(grouped_anagram(["eat", "tea", "tan", "ate", "nat", "bat"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment