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
11 changes: 9 additions & 2 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
def intersection(list1, list2)

Choose a reason for hiding this comment

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

This works, but it's O(n * m) in time complexity. Using a hash you can get this to O(n + m) time complexity which is much better.

I encourage you to think about how to do this.

raise NotImplementedError, "Intersection not implemented"
end
common_items = []
list1.each do |item|
if common_items.include?(item) == false && list2.include?(item) == true

Choose a reason for hiding this comment

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

.include? is an O(n) method and since you have this in a loop intersection becomes an O(n * m) method.

common_items.push(item)
end
end
return common_items

end
12 changes: 11 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

def palindrome_permutation?(string)

Choose a reason for hiding this comment

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

This works, but see my notes on time complexity.

raise NotImplementedError, "palindrome_permutation? not implemented"
string_array = string.split("")
hash_of_letters = {}
string_array.each do |letter|
if hash_of_letters.keys.include?(letter)

Choose a reason for hiding this comment

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

Remember that hash_of_letters.keys returns an array and .include? is an O(n) operation. This would be much better as:

Suggested change
if hash_of_letters.keys.include?(letter)
if hash_of_letters[letter]

This way you take advantage of the O(1) lookup time of a hash.

hash_of_letters[letter] += 1
else
hash_of_letters[letter] = 1
end
end
return true if hash_of_letters.select { |letter,value| value%2 != 0}.keys.length <= 1
return false
end
25 changes: 23 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

See my suggestions regarding improving your runtime.

raise NotImplementedError, "permutations? not implemented"
end
string1_array = string1.split("")
string2_array = string2.split("")
hash_of_letters = {}

string1_array.each do |letter|
if hash_of_letters.keys.include?(letter)

Choose a reason for hiding this comment

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

Suggested change
if hash_of_letters.keys.include?(letter)
if hash_of_letters[letter]

hash_of_letters[letter] += 1
else
hash_of_letters[letter] = 1
end
end

string2_array.each do |letter|
if hash_of_letters.keys.include?(letter)

Choose a reason for hiding this comment

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

Suggested change
if hash_of_letters.keys.include?(letter)
if hash_of_letters[letter] && hash_of_letters[letter] > 0

hash_of_letters[letter] -= 1
else
return false
end
end

return true if hash_of_letters.select { |letter,value| value != 0}.keys.length == 0
return false
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
2 changes: 1 addition & 1 deletion 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 Down