Skip to content

Commit 76b5c95

Browse files
authored
Solve 49 - Group Anagrams (#9)
1 parent 0652b4c commit 76b5c95

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ This is a monorepo containing my solutions to LeetCode problems written in vario
99
## List of Problems/Solutions
1010

1111
- 1 - Two Sum - [Notes](notes/0001_two_sum.md) | [Elixir](elixir/lib/solutions/0001_two_sum/two_sum.ex) | [Ruby](ruby/lib/solutions/0001_two_sum/two_sum.rb)
12+
- 49 - Group Anagrams - [Notes](notes/0049_group_anagrams.md) | [Elixir](elixir/lib/solutions/0049_group_anagrams/group_anagrams.ex) | [Ruby](ruby/lib/solutions/0049_group_anagrams/group_anagrams.rb)
1213
- 217 - Contains Duplicate - [Notes](notes/0217_contains_duplicate.md) | [Elixir](elixir/lib/solutions/0217_contains_duplicate/contains_duplicate.ex) | [Ruby](ruby/lib/solutions/0217_contains_duplicate/contains_duplicate.rb)
1314
- 242 - Valid Anagram - [Notes](notes/0242_valid_anagram.md) | [Elixir](elixir/lib/solutions/0242_valid_anagram/valid_anagram.ex) | [Ruby](ruby/lib/solutions/0242_valid_anagram/valid_anagram.rb)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
defmodule LeetCode.Solutions.GroupAnagrams do
2+
@moduledoc false
3+
4+
@spec call(strs :: [String.t()]) :: [[String.t()]]
5+
def call(strs) do
6+
strs
7+
|> Enum.group_by(fn word ->
8+
word
9+
|> String.graphemes()
10+
|> Enum.sort()
11+
end)
12+
|> Map.values()
13+
end
14+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule LeetCode.Solutions.GroupAnagramsTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LeetCode.Solutions.GroupAnagrams
5+
6+
test "Case 1 works" do
7+
input = ["eat", "tea", "tan", "ate", "nat", "bat"]
8+
expected_output = [["bat"], ["eat", "tea", "ate"], ["tan", "nat"]]
9+
10+
assert GroupAnagrams.call(input) == expected_output
11+
end
12+
13+
test "Case 2 works" do
14+
input = [""]
15+
expected_output = [[""]]
16+
17+
assert GroupAnagrams.call(input) == expected_output
18+
end
19+
20+
test "Case 3 works" do
21+
input = ["a"]
22+
expected_output = [["a"]]
23+
24+
assert GroupAnagrams.call(input) == expected_output
25+
end
26+
end

notes/0049_group_anagrams.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Group Anagrams
2+
3+
**Link to Problem**: https://leetcode.com/problems/group-anagrams
4+
5+
## Solutions
6+
7+
- [Elixir](../elixir/lib/solutions/0049_group_anagrams/group_anagrams.ex)
8+
- [Ruby](../ruby/lib/solutions/0049_group_anagrams/group_anagrams.rb)
9+
10+
## Description
11+
12+
Given an array of strings `strs`, group **the anagrams** together. You can return the answer
13+
in **any order**.
14+
15+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or
16+
phrase, typically using all the original letters exactly once.
17+
18+
## Examples
19+
20+
### Example 1
21+
22+
```
23+
Input: strs = ["eat","tea","tan","ate","nat","bat"]
24+
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
25+
```
26+
27+
### Example 2
28+
29+
```
30+
Input: strs = [""]
31+
Output: [[""]]
32+
```
33+
34+
### Example 3
35+
36+
```
37+
Input: strs = ["a"]
38+
Output: [["a"]]
39+
```
40+
41+
## Approach
42+
43+
The clue for this problem is that we are asked to group the anagrams together and that we have
44+
to product a nested array for the output.
45+
46+
This means that we can make use of the language's `group_by` method/function which produces a
47+
hash map based on the grouping criteria that we pass into it.
48+
49+
Since this is an anagram problem, we just repeat the same solution as
50+
[242 - Valid Anagram](0242_valid_anagram.md) where we determine anagrams by sorting the characters
51+
of a given string.
52+
53+
Once we get the hash map, we just output the values of the hash map without the keys.
54+
55+
## Additional Thoughts
56+
57+
This is a medium problem, but I only got here because it was part of the build up for Blind 75.
58+
59+
The progression to get to this problem is pretty good because I at least had some idea on how to
60+
solve this.
61+
62+
Of course, I didn't actually figure out the solution for this on my own and I had to look it up.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
def group_anagrams(strs)
4+
strs.group_by { |str| str.chars.sort }.values
5+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require_relative '../../../lib/solutions/0049_group_anagrams/group_anagrams'
5+
6+
class GroupAnagramsTest < Minitest::Test
7+
def test_case_1_works
8+
input = %w[eat tea tan ate nat bat]
9+
expected_output = [%w[eat tea ate], %w[tan nat], %w[bat]]
10+
11+
assert_equal expected_output, group_anagrams(input)
12+
end
13+
14+
def test_case_2_works
15+
assert_equal [['']], group_anagrams([''])
16+
end
17+
18+
def test_case_3_works
19+
assert_equal [['a']], group_anagrams(['a'])
20+
end
21+
end

0 commit comments

Comments
 (0)