-
Notifications
You must be signed in to change notification settings - Fork 47
quin - time #29
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?
quin - time #29
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| end | ||
| hash = {} | ||
| array = [] | ||
| list1.each do |num| | ||
| hash[num] = true | ||
| list2.each do |num2| | ||
| if hash[num2] | ||
| array << num2 | ||
| end | ||
| end | ||
| end | ||
| return array.uniq | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,18 @@ | ||
|
|
||
| def palindrome_permutation?(string) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This works |
||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||
| end | ||
| hash = {} | ||
| count = 0 | ||
| string_array = string.split("") | ||
| string_array.each do |char| | ||
| hash[char] = 0 | ||
| end | ||
| string_array.each do |char| | ||
| if hash[char] | ||
| hash[char] += 1 | ||
| end | ||
| if hash[char] % 2 == 0 | ||
| count += 1 | ||
| end | ||
| end | ||
| return count == string_array.length/2 | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,21 @@ | ||
|
|
||
| def permutations?(string1, string2) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't quite work, for example it would fail for |
||
| raise NotImplementedError, "permutations? not implemented" | ||
| end | ||
| hash = {} | ||
| if string1.length != string2.length | ||
| return false | ||
| end | ||
| count = 0 | ||
| string1 = string1.split("") | ||
| string2 = string2.split("") | ||
| hash = {} | ||
| string1.each do |char| | ||
| hash[char] = true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You would do better to count the number of times each letter appears. |
||
| end | ||
| string2.each do |char2| | ||
| if !hash[char2] | ||
| return false | ||
| end | ||
| end | ||
| return true | ||
| 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.
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.