Skip to content

Commit 4295fce

Browse files
committed
Sync LeetCode submission Runtime - 18 ms (39.42%), Memory - 22.1 MB (12.02%)
1 parent 881d46a commit 4295fce

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

0049-group-anagrams/README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
<p>Given an array of strings <code>strs</code>, group <strong>the anagrams</strong> together. You can return the answer in <strong>any order</strong>.</p>
2-
3-
<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.</p>
1+
<p>Given an array of strings <code>strs</code>, group the <span data-keyword="anagram">anagrams</span> together. You can return the answer in <strong>any order</strong>.</p>
42

53
<p>&nbsp;</p>
64
<p><strong class="example">Example 1:</strong></p>
7-
<pre><strong>Input:</strong> strs = ["eat","tea","tan","ate","nat","bat"]
8-
<strong>Output:</strong> [["bat"],["nat","tan"],["ate","eat","tea"]]
9-
</pre><p><strong class="example">Example 2:</strong></p>
10-
<pre><strong>Input:</strong> strs = [""]
11-
<strong>Output:</strong> [[""]]
12-
</pre><p><strong class="example">Example 3:</strong></p>
13-
<pre><strong>Input:</strong> strs = ["a"]
14-
<strong>Output:</strong> [["a"]]
15-
</pre>
5+
6+
<div class="example-block">
7+
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;eat&quot;,&quot;tea&quot;,&quot;tan&quot;,&quot;ate&quot;,&quot;nat&quot;,&quot;bat&quot;]</span></p>
8+
9+
<p><strong>Output:</strong> <span class="example-io">[[&quot;bat&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;ate&quot;,&quot;eat&quot;,&quot;tea&quot;]]</span></p>
10+
11+
<p><strong>Explanation:</strong></p>
12+
13+
<ul>
14+
<li>There is no string in strs that can be rearranged to form <code>&quot;bat&quot;</code>.</li>
15+
<li>The strings <code>&quot;nat&quot;</code> and <code>&quot;tan&quot;</code> are anagrams as they can be rearranged to form each other.</li>
16+
<li>The strings <code>&quot;ate&quot;</code>, <code>&quot;eat&quot;</code>, and <code>&quot;tea&quot;</code> are anagrams as they can be rearranged to form each other.</li>
17+
</ul>
18+
</div>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<div class="example-block">
23+
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;&quot;]</span></p>
24+
25+
<p><strong>Output:</strong> <span class="example-io">[[&quot;&quot;]]</span></p>
26+
</div>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<div class="example-block">
31+
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;a&quot;]</span></p>
32+
33+
<p><strong>Output:</strong> <span class="example-io">[[&quot;a&quot;]]</span></p>
34+
</div>
35+
1636
<p>&nbsp;</p>
1737
<p><strong>Constraints:</strong></p>
1838

0049-group-anagrams/solution.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
# Approach 1 - Categorize by Sorted String
1+
# Approach 2 - Categorize by count
22

3-
# Time: O(n k log k), n = length of strs, k = max lenth of string in strs
4-
# Space: O(n*k)
3+
# Time: O(n * k), n = length of strs, k = max lenth of string in strs
4+
# Space: O(n * k)
55

66
from collections import defaultdict
77

88
class Solution:
99
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
10-
hashmap = defaultdict(list)
11-
10+
ans = defaultdict(list)
11+
1212
for s in strs:
13-
hashmap[tuple(sorted(s))].append(s)
14-
15-
return hashmap.values()
13+
count = [0] * 26
14+
for c in s:
15+
count[(ord(c) - ord('a'))] += 1
16+
ans[tuple(count)].append(s)
17+
18+
return list(ans.values())
1619

0 commit comments

Comments
 (0)