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
16 changes: 15 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# time complexity: O(n + m)
# space complexity: O(n)

def intersection(list1, list2)
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.

👍 Perfect!

raise NotImplementedError, "Intersection not implemented"
hash_table = {}
intersection_array = []

list1.each do |num|
hash_table[num] = 1
end

list2.each do |num|
intersection_array << num if hash_table[num]
end

return intersection_array
end
19 changes: 17 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@

# time complexity: O(n)
# space complexity: O(n)
def palindrome_permutation?(string)
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, "palindrome_permutation? not implemented"
return true if string.length == 1 || string.length == 0

split_string = string.chars
hash_table = {}

split_string.each do |char|
value = hash_table[char]
hash_table[char]? hash_table[char] = value +1 : hash_table[char] = 1
end

odd_count = 0
hash_table.values.each do |value|
odd_count += 1 if value.odd?
end
odd_count <= 1 ? true : false
end
22 changes: 21 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
# time complexity: O(n + m)
# space complexity: O(n)

def permutations?(string1, string2)
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.

👍

raise NotImplementedError, "permutations? not implemented"
return true if string1.empty? && string2.empty?

split_string1 = string1.chars
split_string2 = string2.chars

hash_table = {}

split_string1.each do |char|
value = hash_table[char]
hash_table[char] ? hash_table[char] = value + 1 : hash_table[char] = 1
end

split_string2.each do |char|
value = hash_table[char]
hash_table[char] ? hash_table[char] = value - 1 : false
end

return hash_table.values.sum == 0 && hash_table.values.min == 0 ? true : 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