Skip to content

Conversation

@stephaniejmars
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 mostly 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.

@@ -1,3 +1,3 @@
def intersection(list1, list2)
raise NotImplementedError, "Intersection not implemented"
intersect = list1 & list2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That works as it uses a feature of Ruby. Do think about how you'd solve it with a hash.


letter_hash = {}
string.chars.map do |char|
letter_hash["#{char}"] = string.count(char)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string.count(char) is an O(n) operation, so that makes this a total of O(n^2) operation.

It could be better doing this:

Suggested change
letter_hash["#{char}"] = string.count(char)
letter_hash["#{char}"] = letter_hash["#{char}"].nil? ? 1 : letter_hash["#{char}"] + 1

@@ -1,4 +1,17 @@

def palindrome_permutation?(string)

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

return false if string1.length != string2.length

string1.chars.each do |char|
if string2.include?(char)

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, making this an O(n^2) method. Can you think of a better way to do this with a hash?

@@ -1,4 +1,14 @@

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 fails for heelo and hello, and it's more than a little inefficient.

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