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
26 changes: 25 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
# Time complexity: O(n^2)
# 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.

Your time complexity is O(n + m) where n and m are the lengths of the two arrays. Otherwise well done.

raise NotImplementedError, "Intersection not implemented"
hash = {}
list1.each do |i|
if hash[i/10]
hash[i/10] << i
else
hash[i/10] = [i]
end
end

array = []
list2.each do |i|
if hash[i/10]
hash[i/10].each do |x|
if i == x
array << i
break
end
end
end
end

return array
end
24 changes: 23 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# Time complexity: O(n)
# Space complexity: O(n)

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.

👍 Well done.

raise NotImplementedError, "palindrome_permutation? not implemented"
hash = {}

string.length.times do |i|
if hash[string[i]]
hash[string[i]] += 1
else
hash[string[i]] = 1
end
end


odd = 0
hash.each do |k, v|
if v % 2 != 0
if v == 1
return false if (odd += 1) > 1
end
end
end

return true
end
24 changes: 22 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@

# Time complexity: O(n)
# 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.

👍 Well done

raise NotImplementedError, "permutations? not implemented"
return false if string1.length != string2.length

hash = {}
string1.length.times do |i|
if hash[string1[i]]
hash[string1[i]] += 1
else
hash[string1[i]] = 1
end
end

string2.length.times do |i|
if hash[string2[i]] && hash[string2[i]] > 0
hash[string2[i]] -= 1
else
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