Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 35afea5

Browse files
49. Group Anagrams
1 parent a481709 commit 35afea5

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

‎ArraysAndHashing/Medium/49_Group_Anagrams.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
'''
3939

4040
# Brute Force
41-
# Total Complexity: O(N log N + N K log K)
41+
# Total Complexity: O(N log N + N K log K) ≈ O(NKlogK)
4242
# Space Complexity: O(N K)
4343

4444
'''
@@ -61,6 +61,20 @@ def groupAnagrams(self, strs):
6161

6262
return list(mapp.values())
6363

64+
#Bit clear
65+
66+
from collections import defaultdict
67+
class Solution:
68+
def groupAnagrams(self, strs):
69+
mapp=defaultdict(list)
70+
71+
for s in strs:
72+
sorted_key ="".join(sorted(s))
73+
mapp[sorted_key].append(s)
74+
75+
return list(mapp.values())
76+
77+
6478
# Optimized Solution
6579
# Time Complexity: O(m*n)
6680
# Space Complexity: O(m)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /