-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Space - Katie #36
base: master
Are you sure you want to change the base?
Space - Katie #36
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
common_items = [] | ||
list1.each do |item| | ||
if common_items.include?(item) == false && list2.include?(item) == true |
There was a problem hiding this comment.
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) 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.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but see my notes on time complexity.
|
||
string1_array.each do |letter| | ||
if hash_of_letters.keys.include?(letter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if hash_of_letters.keys.include?(letter) | |
if hash_of_letters[letter] |
|
||
string2_array.each do |letter| | ||
if hash_of_letters.keys.include?(letter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my suggestions regarding improving your runtime.
No description provided.