Skip to content

Commit 1e524a2

Browse files
committed
update readme
1 parent b962037 commit 1e524a2

File tree

5 files changed

+43
-103
lines changed

5 files changed

+43
-103
lines changed

0049-group-anagrams/0049-group-anagrams.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

0049-group-anagrams/README.md

Lines changed: 0 additions & 43 deletions
This file was deleted.

Python/0049-group-anagrams.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
# time complexity: O(nklogk)
2-
# space complexity: O(nk)
1+
# time complexity: O(nlogn * n)
2+
# space complexity: O(n)
33
from collections import defaultdict
44
from typing import List
55

66

77
class Solution:
88
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
9-
ans = defaultdict(list)
10-
for s in strs:
11-
ans[tuple(sorted(s))].append(s)
12-
return ans.values()
9+
wordMap = defaultdict(list)
10+
for word in strs:
11+
key = ''.join(sorted(word))
12+
wordMap[key].append(word)
13+
return [row for row in wordMap.values()]
1314

1415

1516
strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
1617
print(Solution().groupAnagrams(strs))
18+
strs = [""]
19+
print(Solution().groupAnagrams(strs))
20+
strs = ["a"]
21+
print(Solution().groupAnagrams(strs))

README.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,3 @@ By following these steps, users can easily navigate and utilize your LeetCode so
8080
## Contributing
8181

8282
We welcome contributions! If you have a solution to a problem that isn't already included, or if you have suggestions for improvements, please feel free to submit a pull request.
83-
84-
<!---LeetCode Topics Start-->
85-
# LeetCode Topics
86-
## Array
87-
| |
88-
| ------- |
89-
| [0049-group-anagrams](https://github.com/hogan-tech/leetcode-solution/tree/master/0049-group-anagrams) |
90-
## Hash Table
91-
| |
92-
| ------- |
93-
| [0049-group-anagrams](https://github.com/hogan-tech/leetcode-solution/tree/master/0049-group-anagrams) |
94-
## String
95-
| |
96-
| ------- |
97-
| [0049-group-anagrams](https://github.com/hogan-tech/leetcode-solution/tree/master/0049-group-anagrams) |
98-
## Sorting
99-
| |
100-
| ------- |
101-
| [0049-group-anagrams](https://github.com/hogan-tech/leetcode-solution/tree/master/0049-group-anagrams) |
102-
<!---LeetCode Topics End-->

Readme/0049-group-anagrams.md

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
<h2><a href="https://leetcode.com/problems/group-anagrams/">49. Group Anagrams</a></h2><h3>Medium</h3><hr><div><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+
<h2><a href="https://leetcode.com/problems/group-anagrams">49. Group Anagrams</a></h2><h3>Medium</h3><hr><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

@@ -21,4 +41,3 @@
2141
<li><code>0 &lt;= strs[i].length &lt;= 100</code></li>
2242
<li><code>strs[i]</code> consists of lowercase English letters.</li>
2343
</ul>
24-
</div>

0 commit comments

Comments
 (0)