From 0e053a6b033d121d79a84d78cf07e3b52e4b341a Mon Sep 17 00:00:00 2001 From: Angela Nguyen Date: Mon, 30 Mar 2020 01:29:54 -0700 Subject: [PATCH 1/3] all tests passing --- lib/array_intersection.rb | 16 +++++++++++++++- lib/palindrome_permutation.rb | 13 ++++++++++++- lib/permutations.rb | 18 +++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..04cef67 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,17 @@ +# Takes two integer arrays as arguments and returns a new array of their intersecting numbers. +#Time complexity: O(n), worse case scenario we iterate over every item in both lists +#Space complexity: O(n), creating a new hash and a new array def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + integer_hash = {} + + list1.each do |int| + integer_hash[int] = 1 + end + + intersection = [] + list2.each do |int| + intersection << int if integer_hash.has_key?(int) + end + + return intersection end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..e09abb0 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,15 @@ +# Takes a string as an argument and checks if it could be a valid palindrome. +# Valid palindromes have a maximum of one letter that appears an odd number of times. +# Time: O(n), we iterate over the string once and over our hash once +# Space: O(n), worse case scenario we create a new hash collection that's as "large" as our string def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + hash = {} + + string.chars.each do |letter| + hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1 + end + + return hash.select{ |k,v| v.odd? }.size <= 1 + end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..10cc9db 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,20 @@ +# Accepts two strings as arguments and checks if they're permutations of each other. +# Permutations share the same letter counts between each other. +# Time: O(n), we iterate through three different collections of fixed size +# Space: O(n), we create a new hash + def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + hash = {} + + string1.chars.each do |letter| + hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1 + end + + string2.chars.each do |letter| + return false unless hash.has_key?(letter) && hash[letter] > 0 + hash[letter] -= 1 + end + + hash.any? {|key, value| value > 0} ? false : true end \ No newline at end of file From ce3e7191bb0327c51b85f5960de387e8be6ce0f3 Mon Sep 17 00:00:00 2001 From: Angela Nguyen Date: Mon, 30 Mar 2020 01:49:38 -0700 Subject: [PATCH 2/3] optimize to catch early fail cases without having to iterate over entire collections --- lib/palindrome_permutation.rb | 10 ++++++++-- lib/permutations.rb | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index e09abb0..ce9524a 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -10,6 +10,12 @@ def palindrome_permutation?(string) hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1 end - return hash.select{ |k,v| v.odd? }.size <= 1 - + pivot_letters = 0 + + hash.each do |k, v| + pivot_letters += 1 if v.odd? + return false if pivot_letters > 1 #catch early cases of finding more than 1 pivot letter + end + + return true end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 10cc9db..dc8d0fa 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -12,7 +12,7 @@ def permutations?(string1, string2) end string2.chars.each do |letter| - return false unless hash.has_key?(letter) && hash[letter] > 0 + return false unless hash.has_key?(letter) && hash[letter] > 0 #quickly catch obvious fail cases hash[letter] -= 1 end From 9d9f56179767f90521f5adc7f582f771b2de182f Mon Sep 17 00:00:00 2001 From: Angela Nguyen Date: Tue, 31 Mar 2020 03:06:24 -0700 Subject: [PATCH 3/3] cleaned up funky syntax --- lib/permutations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index dc8d0fa..97e554d 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -16,5 +16,5 @@ def permutations?(string1, string2) hash[letter] -= 1 end - hash.any? {|key, value| value > 0} ? false : true + return !(hash.any? {|key, value| value > 0}) end \ No newline at end of file