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 @@
# Takes two integer arrays as arguments and returns a new array of their intersecting numbers.
#Time complexity: O(n), worse case scenario we iterate over every item in both lists
#Space complexity: O(n), creating a new hash and a new array
def intersection(list1, list2)
Comment on lines +2 to 4

Choose a reason for hiding this comment

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

👍 And correct time/space complexity

raise NotImplementedError, "Intersection not implemented"
integer_hash = {}

list1.each do |int|
integer_hash[int] = 1
end

intersection = []
list2.each do |int|
intersection << int if integer_hash.has_key?(int)
end

return intersection
end
19 changes: 18 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# Takes a string as an argument and checks if it could be a valid palindrome.
# Valid palindromes have a maximum of one letter that appears an odd number of times.
# Time: O(n), we iterate over the string once and over our hash once
# Space: O(n), worse case scenario we create a new hash collection that's as "large" as our string

def palindrome_permutation?(string)
Comment on lines +3 to 6

Choose a reason for hiding this comment

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

👍 Nicely done.

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

string.chars.each do |letter|
hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1
end

pivot_letters = 0

hash.each do |k, v|
pivot_letters += 1 if v.odd?
return false if pivot_letters > 1 #catch early cases of finding more than 1 pivot letter
end

return true
end
18 changes: 17 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Accepts two strings as arguments and checks if they're permutations of each other.
# Permutations share the same letter counts between each other.
# Time: O(n), we iterate through three different collections of fixed size
# Space: O(n), we create a new hash


def permutations?(string1, string2)
Comment on lines +1 to 7

Choose a reason for hiding this comment

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

👍 nice work!

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

string1.chars.each do |letter|
hash.has_key?(letter) ? hash[letter] += 1 : hash[letter] = 1
end

string2.chars.each do |letter|
return false unless hash.has_key?(letter) && hash[letter] > 0 #quickly catch obvious fail cases
hash[letter] -= 1
end

return !(hash.any? {|key, value| value > 0})
end