From 1e4d510748582d219d670a29917702de9efc4805 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 26 Mar 2021 10:43:21 -0700 Subject: [PATCH 1/4] Add anagram checker challenge --- DIRECTORY.md | 1 + data_structures/arrays/anagram_checker.rb | 31 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 data_structures/arrays/anagram_checker.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index 8fea99b..fe034ed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -8,6 +8,7 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) + * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/anagram_checker.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) diff --git a/data_structures/arrays/anagram_checker.rb b/data_structures/arrays/anagram_checker.rb new file mode 100644 index 0000000..d082a36 --- /dev/null +++ b/data_structures/arrays/anagram_checker.rb @@ -0,0 +1,31 @@ +# Challenge name: Is anagram +# +# Given two strings s and t , write a function to determine +# if t is an anagram of s. +# +# Note: +# You may assume the string contains only lowercase alphabets. +# +# Follow up: +# What if the inputs contain unicode characters? +# How would you adapt your solution to such case? +# +# @param {String} s +# @param {String} t +# @return {Boolean} + +def is_anagram(s, t) +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 \ No newline at end of file From 6c785524fbad57ffb26d8858fdfb2f348a4fd296 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Tue, 30 Mar 2021 14:19:43 -0700 Subject: [PATCH 2/4] Add sort and compare solution --- DIRECTORY.md | 2 +- .../arrays/{ => strings}/anagram_checker.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) rename data_structures/arrays/{ => strings}/anagram_checker.rb (76%) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3ae4c04..0014e38 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -8,7 +8,6 @@ ## Data Structures * Arrays * [Add Digits](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/add_digits.rb) - * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/anagram_checker.rb) * [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_all_duplicates_in_an_array.rb) * [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb) * [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb) @@ -19,6 +18,7 @@ * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings + * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) diff --git a/data_structures/arrays/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb similarity index 76% rename from data_structures/arrays/anagram_checker.rb rename to data_structures/arrays/strings/anagram_checker.rb index d082a36..b66eb3f 100644 --- a/data_structures/arrays/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -14,7 +14,18 @@ # @param {String} t # @return {Boolean} +# +# Approach 1: Sort and Compare +# +# Time Complexity: O(n log n) +# def is_anagram(s, t) + return false if s.length != t.length + + arr1 = s.split('').sort + arr2 = t.split('').sort + + arr1 == arr2 end s = 'anagram' From 3bc81b8195b91a2282b7eb4b3fdc916364ec373f Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 18:37:57 -0700 Subject: [PATCH 3/4] Update data_structures/arrays/strings/anagram_checker.rb --- data_structures/arrays/strings/anagram_checker.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/strings/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb index b66eb3f..71f59d6 100644 --- a/data_structures/arrays/strings/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -17,7 +17,9 @@ # # Approach 1: Sort and Compare # -# Time Complexity: O(n log n) +# Complexity analysis: +# +# Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n). # def is_anagram(s, t) return false if s.length != t.length @@ -39,4 +41,4 @@ puts(is_anagram(s, t)) s = 'a' t = 'ab' puts(is_anagram(s, t)) -# => false \ No newline at end of file +# => false From a93d5cc072d98981331661186ad8650405dc6bde Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Wed, 31 Mar 2021 18:38:01 -0700 Subject: [PATCH 4/4] Update data_structures/arrays/strings/anagram_checker.rb --- data_structures/arrays/strings/anagram_checker.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/strings/anagram_checker.rb b/data_structures/arrays/strings/anagram_checker.rb index 71f59d6..bcd5e36 100644 --- a/data_structures/arrays/strings/anagram_checker.rb +++ b/data_structures/arrays/strings/anagram_checker.rb @@ -20,6 +20,7 @@ # Complexity analysis: # # Time Complexity: O(n log n). Assume that n is the length of s, sorting costs O(n log n), and comparing two strings costs O(n). Sorting time dominates and the overall time complexity is O(n log n). +# Space Complexity: O(1). Space depends on the sorting implementation which, usually, costs O(1) auxiliary space if heapsort is used. # def is_anagram(s, t) return false if s.length != t.length