diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..2bad0d6 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,38 @@ +#Time complexity: O(n+m) +#Space complexity: O(n) def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + + #create a hash with each element of the array as keys and value as true + hash = {} + list1.each do |element| + hash[element] = true + end + #find each element of list2 by the hash's keys + result = [] + list2.each do |element| + if hash[element] == true + result << element + end + end + return result +end + +# Without using the hash table +# def intersection(list1, list2) +# result = [] +# if list1.length >= list2.length +# list1.length.times do |i| +# if list1.include?(list2[i]) +# result << list2[i] +# end +# end +# return result +# else +# list2.length.times do |i| +# if list2.include?(list1[i]) +# result << list1[i] +# end +# end +# return result +# end +# end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..fdb6f22 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,30 @@ +#Time complexity: O(n+m+l) +#Space complexity: O(n^2) def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + return true if string == "" + + #Convert the string to an array of letters + string_array = string.chars + + #reverse_string_array + reverse_string_array = [] + string_array.each do |letter| + reverse_string_array.unshift(letter) + end + + #create a string_hash by having each element string_array as keys + string_hash = {} + + string_array.length.times do |i| + string_hash[string_array[i]] = reverse_string_array[i] + end + + #compare each key and value of the hash + string_hash.each do |key, value| + if key == value + return true + end + end + return false end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..54429d5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,23 @@ - +#Time complexity: O(n+m) +#Space complexity: O(n) def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return false if (string1.length != string2.length) + + #convert both strings to arrays of letters + array1 = string1.chars + array2 = string2.chars + + #create a hash by assigning each element of array1 for the keys of hash + + hash = {} + array1.each do |letter| + hash[letter] = true + end + + array2.each do |letter| + if hash[letter] != true + return false + end + end + return true end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..984b333 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "palindrome_permutation?" do +describe "palindrome_permutation?" do it "will work for hello" do expect(palindrome_permutation?("hello")).must_equal false end diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..ada5ee1 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -1,6 +1,6 @@ require_relative "test_helper" -xdescribe "permutations?" do +describe "permutations?" do it "returns true for empty string" do expect(permutations?("", "")).must_equal true end @@ -13,7 +13,7 @@ expect(permutations?("pasta", "atsap")).must_equal true end - it "returns true for 'pizza', 'pizza'" do + it "returns true for 'pizza', 'pasta'" do expect(permutations?("pizza", "pasta")).must_equal false end