From dc533283a2a3c5743fae1548210f8968fb7a8d08 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:36:46 -0700 Subject: [PATCH 1/3] Add hash table approach --- data_structures/hash_table/anagram_checker.rb | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 1158134..0d0f2f6 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -17,8 +17,6 @@ # # Approach: Hash table # - -# # Complexity analysis: # # Time complexity: O(n). Time complexity is O(n) since accessing the counter @@ -60,3 +58,74 @@ s = 'a' t = 'ab' puts(is_anagram(s, t)) # => false + + +def is_anagram(s, t) + s_length = s.length + t_length = t.length + counter = Hash.new(0) + + return false unless s_length == t_length + + (0...s_length).each do |i| + counter[s[i]] += 1 + end + + (0...s_length).each do |i| + counter[t[i]] -= 1 + + return false if counter[t[i]] < 0 + end + + true +end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false + +# +# Approach 2: Hash table +# + +# Algorithm: we could also first increment the counter for s, +# then decrement the counter for t. If at any point the counter +# drops below zero, we know that t contains an extra letter, +# not in s, and return false immediately. + +# Complexity analysis: +# +# Time complexity: O(n). +# Space complexity: O(1). +# + +def is_anagram(s, t) + s_length = s.length + t_length = t.length + counter = Hash.new(0) + + return false unless s_length == t_length + + (0...s_length).each do |i| + counter[s[i]] += 1 + end + + (0...s_length).each do |i| + counter[t[i]] -= 1 + + return false if counter[t[i]] < 0 + end + + true +end From 44c32084541b2679161577a1257d1454d4f4752d Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:36:52 -0700 Subject: [PATCH 2/3] Add another hash table approach --- data_structures/hash_table/anagram_checker.rb | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 0d0f2f6..16fc429 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -129,3 +129,38 @@ def is_anagram(s, t) true end + +# +# Approach 3: populate 2 hashes and compare them +# + +def is_anagram(s, t) + s = s.chars + t = t.chars + + return false if s.count != t.count + + hash1 = {} + s.chars.each do |value| + hash1[value] = if hash1[value] + hash1[value] + 1 + else + 1 + end + end + + hash2 = {} + t.chars.each do |value| + hash2[value] = if hash2[value] + hash2[value] + 1 + else + 1 + end + end + + hash1.keys.each do |key| + return false if hash2[key] != hash1[key] + end + + true +end From 434e72a4f7cc0cd977e98c941449e492929e1d2f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Thu, 1 Apr 2021 09:38:27 -0700 Subject: [PATCH 3/3] Add very simple tests --- data_structures/hash_table/anagram_checker.rb | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/data_structures/hash_table/anagram_checker.rb b/data_structures/hash_table/anagram_checker.rb index 16fc429..abbb209 100644 --- a/data_structures/hash_table/anagram_checker.rb +++ b/data_structures/hash_table/anagram_checker.rb @@ -130,6 +130,21 @@ def is_anagram(s, t) true end +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false + # # Approach 3: populate 2 hashes and compare them # @@ -141,7 +156,7 @@ def is_anagram(s, t) return false if s.count != t.count hash1 = {} - s.chars.each do |value| + s.each do |value| hash1[value] = if hash1[value] hash1[value] + 1 else @@ -150,7 +165,7 @@ def is_anagram(s, t) end hash2 = {} - t.chars.each do |value| + t.each do |value| hash2[value] = if hash2[value] hash2[value] + 1 else @@ -164,3 +179,18 @@ def is_anagram(s, t) true end + +s = 'anagram' +t = 'nagaram' +puts(is_anagram(s, t)) +# => true + +s = 'rat' +t = 'car' +puts(is_anagram(s, t)) +# => false + +s = 'a' +t = 'ab' +puts(is_anagram(s, t)) +# => false