-
Notifications
You must be signed in to change notification settings - Fork 47
Alex - Time #39
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?
Alex - Time #39
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,11 @@ | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
|
|
||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| crossroads = [] | ||
|
|
||
| list1.each do |n| | ||
| crossroads << n if list2.include?(n) != false | ||
|
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.
|
||
| end | ||
| return crossroads | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,26 @@ | ||||||
| # Time Complexity: O(n) | ||||||
| # Space Complexity: O(n) | ||||||
|
|
||||||
| def palindrome_permutation?(string) | ||||||
|
Comment on lines
+1
to
4
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. 👍 |
||||||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||||||
| end | ||||||
| while string.empty? | ||||||
|
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. while?
Suggested change
|
||||||
| return true | ||||||
| end | ||||||
|
|
||||||
| board = {} | ||||||
| letters = string.chars | ||||||
| chalk = 0 | ||||||
|
|
||||||
| letters.each do |c| | ||||||
| board[c] = 0 | ||||||
| end | ||||||
|
|
||||||
| letters.each do |c| | ||||||
| if board[c] | ||||||
| board[c] += 1 | ||||||
| end | ||||||
| if board[c] % 2 == 0 | ||||||
| chalk += 1 | ||||||
| end | ||||||
| end | ||||||
| return chalk == letters.length/2 | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
|
|
||
| def permutations?(string1, string2) | ||
|
Comment on lines
+1
to
4
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 only if the count of each letter is the same. It returns true however for |
||
| raise NotImplementedError, "permutations? not implemented" | ||
| if string1.length != string2.length | ||
| return false | ||
| end | ||
|
|
||
| perm = string1.chars | ||
| mutant = string2.chars | ||
| mindflayer = {} | ||
|
Comment on lines
+9
to
+11
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. I like DND too, but these aren't the best variable names. |
||
|
|
||
| perm.each do |c| | ||
| mindflayer[c] = true | ||
| end | ||
|
|
||
| mutant.each do |c| | ||
| if mindflayer[c] != true | ||
| 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.