Skip to content

Commit

Permalink
Sync LeetCode submission Runtime - 18 ms (39.42%), Memory - 22.1 MB (…
Browse files Browse the repository at this point in the history
…12.02%)
  • Loading branch information
jimit105 committed Dec 17, 2024
1 parent 881d46a commit 4295fce
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
44 changes: 32 additions & 12 deletions 0049-group-anagrams/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
<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>

<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>
<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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> strs = ["eat","tea","tan","ate","nat","bat"]
<strong>Output:</strong> [["bat"],["nat","tan"],["ate","eat","tea"]]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> strs = [""]
<strong>Output:</strong> [[""]]
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> strs = ["a"]
<strong>Output:</strong> [["a"]]
</pre>

<div class="example-block">
<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>

<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>

<p><strong>Explanation:</strong></p>

<ul>
<li>There is no string in strs that can be rearranged to form <code>&quot;bat&quot;</code>.</li>
<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>
<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>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;&quot;]]</span></p>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">strs = [&quot;a&quot;]</span></p>

<p><strong>Output:</strong> <span class="example-io">[[&quot;a&quot;]]</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

Expand Down
19 changes: 11 additions & 8 deletions 0049-group-anagrams/solution.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Approach 1 - Categorize by Sorted String
# Approach 2 - Categorize by count

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

from collections import defaultdict

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
hashmap = defaultdict(list)
ans = defaultdict(list)

for s in strs:
hashmap[tuple(sorted(s))].append(s)

return hashmap.values()
count = [0] * 26
for c in s:
count[(ord(c) - ord('a'))] += 1
ans[tuple(count)].append(s)

return list(ans.values())

0 comments on commit 4295fce

Please sign in to comment.