Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Your solutions mostly work but have less than ideal time complexity. Think about how you can use a hash to solve them.
Take a look at my comments and let me know if you have any questions.
| @@ -1,3 +1,3 @@ | |||
| def intersection(list1, list2) | |||
| raise NotImplementedError, "Intersection not implemented" | |||
| intersect = list1 & list2 | |||
There was a problem hiding this comment.
That works as it uses a feature of Ruby. Do think about how you'd solve it with a hash.
|
|
||
| letter_hash = {} | ||
| string.chars.map do |char| | ||
| letter_hash["#{char}"] = string.count(char) |
There was a problem hiding this comment.
string.count(char) is an O(n) operation, so that makes this a total of O(n^2) operation.
It could be better doing this:
| letter_hash["#{char}"] = string.count(char) | |
| letter_hash["#{char}"] = letter_hash["#{char}"].nil? ? 1 : letter_hash["#{char}"] + 1 |
| @@ -1,4 +1,17 @@ | |||
|
|
|||
| def palindrome_permutation?(string) | |||
| return false if string1.length != string2.length | ||
|
|
||
| string1.chars.each do |char| | ||
| if string2.include?(char) |
There was a problem hiding this comment.
.include? is an O(n) operation, making this an O(n^2) method. Can you think of a better way to do this with a hash?
| @@ -1,4 +1,14 @@ | |||
|
|
|||
| def permutations?(string1, string2) | |||
There was a problem hiding this comment.
This fails for heelo and hello, and it's more than a little inefficient.
No description provided.