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 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.
.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.
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.
.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.
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.