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
10 changes: 9 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Time Complexity: O(n)
# Space Complexity: O(n)

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"
crossroads = []

list1.each do |n|
crossroads << n if list2.include?(n) != false

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) operation and you have this in a loop...

end
return crossroads
end
26 changes: 24 additions & 2 deletions 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.

👍

raise NotImplementedError, "palindrome_permutation? not implemented"
end
while string.empty?

Choose a reason for hiding this comment

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

while?

Suggested change
while string.empty?
if string.empty?

return true
end

board = {}
letters = string.chars
chalk = 0

letters.each do |c|
board[c] = 0
end

letters.each do |c|
if board[c]
board[c] += 1
end
if board[c] % 2 == 0
chalk += 1
end
end
return chalk == letters.length/2
end
21 changes: 20 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Time Complexity: O(n)
# 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.

This works only if the count of each letter is the same. It returns true however for heelo and hello.

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

perm = string1.chars
mutant = string2.chars
mindflayer = {}
Comment on lines +9 to +11

Choose a reason for hiding this comment

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

I like DND too, but these aren't the best variable names.


perm.each do |c|
mindflayer[c] = true
end

mutant.each do |c|
if mindflayer[c] != 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
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