Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

LEETCODE-GROUP ANAGRAMS #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions LeetCode Problems/leetcode-groupAnagrams.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
ans=[]
dict1 = {}
if strs==[""]:
return [[""]]
elif len(strs) == 1:
return [strs]
else :

for word in strs:
sorted_word = "".join(sorted(word))
if sorted_word not in dict1:
dict1[sorted_word]=[word]
else:
dict1[sorted_word].append(word)

for words in dict1.values():
ans.append(words)
return ans

dict1= {}
for word in strs:
sorted_word = "".join(sorted(word))
if sorted_word not in dict1:
dict1[sorted_word]=[word]
else:
dict1[sorted_word].append(word)
result = []
for words in dict1.values():
result.append(words)
return result