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
14 changes: 12 additions & 2 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
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
hash = {}
array = []
list1.each do |num|
hash[num] = true
list2.each do |num2|
if hash[num2]
array << num2
end
end
end
return array.uniq
end
18 changes: 16 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@

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

raise NotImplementedError, "palindrome_permutation? not implemented"
end
hash = {}
count = 0
string_array = string.split("")
string_array.each do |char|
hash[char] = 0
end
string_array.each do |char|
if hash[char]
hash[char] += 1
end
if hash[char] % 2 == 0
count += 1
end
end
return count == string_array.length/2
end
21 changes: 19 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

This doesn't quite work, for example it would fail for heelo and hello. See my note below.

raise NotImplementedError, "permutations? not implemented"
end
hash = {}
if string1.length != string2.length
return false
end
count = 0
string1 = string1.split("")
string2 = string2.split("")
hash = {}
string1.each do |char|
hash[char] = true

Choose a reason for hiding this comment

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

You would do better to count the number of times each letter appears.

end
string2.each do |char2|
if !hash[char2]
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
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