Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Space - Katie #36

wants to merge 1 commit into from

Conversation

ktvoort123
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'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

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)

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)

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:

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

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)

Choose a reason for hiding this comment

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

Suggested change
if hash_of_letters.keys.include?(letter)
if hash_of_letters[letter]


string2_array.each do |letter|
if hash_of_letters.keys.include?(letter)

Choose a reason for hiding this comment

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

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

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.

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