Skip to content

Commit 0652b4c

Browse files
authored
Solve 242 - Valid Anagram (#8)
1 parent 6cbb4c1 commit 0652b4c

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ This is a monorepo containing my solutions to LeetCode problems written in vario
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)
1212
- 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)
13+
- 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.ValidAnagram do
2+
@moduledoc false
3+
4+
@spec call(s :: String.t(), t :: String.t()) :: boolean
5+
def call(s, t) do
6+
anagram?(s) == anagram?(t)
7+
end
8+
9+
defp anagram?(word) do
10+
word
11+
|> String.graphemes()
12+
|> Enum.sort()
13+
end
14+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule LeetCode.Solutions.ValidAnagramTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LeetCode.Solutions.ValidAnagram
5+
6+
test "Case 1 works" do
7+
assert ValidAnagram.call("anagram", "nagaram") == true
8+
end
9+
10+
test "Case 2 works" do
11+
assert ValidAnagram.call("rat", "car") == false
12+
end
13+
end

notes/0242_valid_anagram.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Valid Anagram
2+
3+
**Link to Problem**: https://leetcode.com/problems/valid-anagram
4+
5+
## Solutions
6+
7+
- [Elixir](../elixir/lib/solutions/0242_valid_anagram/valid_anagram.ex)
8+
- [Ruby](../ruby/lib/solutions/0242_valid_anagram/valid_anagram.rb)
9+
10+
## Description
11+
12+
Given two strings `s` and `t`, return `true` if `t` is an anagram of `s`, and `false` otherwise.
13+
14+
An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
15+
16+
## Examples
17+
18+
### Example 1
19+
20+
```
21+
Input: s = "anagram", t = "nagaram"
22+
Output: true
23+
```
24+
25+
### Example 2
26+
27+
```
28+
Input: s = "rat", t = "car"
29+
Output: false
30+
```
31+
32+
## Approach
33+
34+
The trick is to sort the words by their characters and compare the two.
35+
36+
## Thoughts
37+
38+
The solutions for both Elixir and Ruby look about the same, but Elixir is bit more verbose since it's
39+
a functional language.
40+
41+
The Elixir solution is a redo from my previous attempt at it in my [old LeetCode repo](https://github.com/terenceponce/leetcode-elixir/tree/main/lib/solutions/00242_valid_anagram).
42+
I have a longer write up about this problem in the old repo, but I won't copy it over here since that
43+
feeling of solving it for the first time is gone now.
44+
45+
The Ruby solution is new though and it's the reason why I rewrote my solution for Elixir. Going a bit
46+
off-topic here, but the reason why I had to make a new repo is so I can practice LeetCode in other
47+
languages.
48+
49+
As for the reason why I want to practice in other languages, I applied to Amazon recently and I had a
50+
rude awakening when I encountered their Online Assessment. It was done through HackerRank and while I
51+
was able to verify that HackerRank does support Elixir, apparently, companies can restrict the languages
52+
available.
53+
54+
Unfortunately, Elixir was not part of the available languages to use for the assessment, so I had to
55+
scramble and figure out how to do LeetCode in Ruby. I haven't done the assessment yet, but I'm pretty
56+
sure I'll fail it because of the fact that I have done all of my preparations exclusively in Elixir.
57+
58+
Regardless, I think it's a good exercise for me to practice LeetCode using both OOP and Functional
59+
Programming, so starting this repo is already worth it.
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 anagram?(word1, word2)
4+
word1.chars.sort == word2.chars.sort
5+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require_relative '../../../lib/solutions/0242_valid_anagram/valid_anagram'
5+
6+
class ValidAnagramTest < Minitest::Test
7+
def test_case_1_works
8+
assert anagram?('anagram', 'nagaram')
9+
end
10+
11+
def test_case_2_works
12+
refute anagram?('rat', 'car')
13+
end
14+
end

0 commit comments

Comments
 (0)