Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
#Time complexity: O(n+m)
#Space complexity: O(n)
def intersection(list1, list2)
Comment on lines +1 to 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Intersection not implemented"
end

#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
28 changes: 27 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
#Time complexity: O(n+m+l)
#Space complexity: O(n^2)

def palindrome_permutation?(string)
Comment on lines +1 to 4

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time complexity is O(n^2) because unshift has O(n) and you're doing it n times. The space complexity O(n) as well.

You seem to be trying to check to see if the string is a palindrome. The problem states that you are checking to see if the letters could be re-arranged into a palindrome.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah...I went back to see the problem. I misunderstood the problem. My mind was thinking about the palindrome that I did a few weeks ago.

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
23 changes: 21 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

#Time complexity: O(n+m)
#Space complexity: O(n)
def permutations?(string1, string2)
Comment on lines +1 to 3

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail for heelo and hello. You should instead count the number of times each letter occurs.

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
2 changes: 1 addition & 1 deletion test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/permutations_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down