Skip to content

Conversation

@halahaddad1
Copy link

No description provided.

Copy link

@CheezItMan CheezItMan left a 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)

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)

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.

Comment on lines +14 to +16
string.each_char do |char|
count[char]= string.count(char)
end

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.

Suggested change
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)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants