Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Your solutions work but have less than ideal time complexity. Think about how you can use a hash's O(1) lookup time to solve them.
Take a look at my comments and let me know if you have any questions.
| end No newline at end of file | ||
| common_items = [] | ||
| list1.each do |item| | ||
| if common_items.include?(item) == false && list2.include?(item) == true |
There was a problem hiding this comment.
.include? is an O(n) method and since you have this in a loop intersection becomes an O(n * m) method.
| @@ -1,3 +1,10 @@ | |||
| def intersection(list1, list2) | |||
There was a problem hiding this comment.
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.
| string_array = string.split("") | ||
| hash_of_letters = {} | ||
| string_array.each do |letter| | ||
| if hash_of_letters.keys.include?(letter) |
There was a problem hiding this comment.
Remember that hash_of_letters.keys returns an array and .include? is an O(n) operation. This would be much better as:
| if hash_of_letters.keys.include?(letter) | |
| if hash_of_letters[letter] |
This way you take advantage of the O(1) lookup time of a hash.
| @@ -1,4 +1,14 @@ | |||
|
|
|||
| def palindrome_permutation?(string) | |||
There was a problem hiding this comment.
This works, but see my notes on time complexity.
| hash_of_letters = {} | ||
|
|
||
| string1_array.each do |letter| | ||
| if hash_of_letters.keys.include?(letter) |
There was a problem hiding this comment.
| if hash_of_letters.keys.include?(letter) | |
| if hash_of_letters[letter] |
| end | ||
|
|
||
| string2_array.each do |letter| | ||
| if hash_of_letters.keys.include?(letter) |
There was a problem hiding this comment.
| if hash_of_letters.keys.include?(letter) | |
| if hash_of_letters[letter] && hash_of_letters[letter] > 0 |
| @@ -1,4 +1,25 @@ | |||
|
|
|||
| def permutations?(string1, string2) | |||
There was a problem hiding this comment.
See my suggestions regarding improving your runtime.
No description provided.