-
Notifications
You must be signed in to change notification settings - Fork 47
Space - Hala #37
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
base: master
Are you sure you want to change the base?
Space - Hala #37
Conversation
CheezItMan
left a comment
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 to solve them.
Take a look at my comments and let me know if you have any questions.
| hash = {} | ||
| shortlist.each do |item| | ||
| if | ||
| longest.include?(item) |
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) operation and since you have this in an O(m) loop... this becomes O(n * m)
Think about how you could use a hash to make this faster.
|
|
||
|
|
||
|
|
||
| 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.each_char do |char| | ||
| count[char]= string.count(char) | ||
| end |
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.
.count has an O(n) loop, so this is an O(n^2) algorithm... Another place a hash could make this better.
| string.each_char do |char| | |
| count[char]= string.count(char) | |
| end | |
| string.each_char do |char| | |
| count[char]= count[char] ? count[char] + 1 : 1 | |
| end |
| # ``` | ||
|
|
||
|
|
||
| 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.
This is another instance where you've brute-forced the solution. It works, but a hash would make this much better.
I encourage you to think a bit further on this.
No description provided.