-
Notifications
You must be signed in to change notification settings - Fork 47
Space - Lak #24
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?
Space - Lak #24
Changes from all commits
e6b6e8a
ec658dd
aadd1b8
321b1cd
05f9c96
90e54da
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,38 @@ | ||
| #Time complexity: O(n+m) | ||
| #Space complexity: O(n) | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| end | ||
|
|
||
| #create a hash with each element of the array as keys and value as true | ||
| hash = {} | ||
| list1.each do |element| | ||
| hash[element] = true | ||
| end | ||
| #find each element of list2 by the hash's keys | ||
| result = [] | ||
| list2.each do |element| | ||
| if hash[element] == true | ||
| result << element | ||
| end | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| # Without using the hash table | ||
| # def intersection(list1, list2) | ||
| # result = [] | ||
| # if list1.length >= list2.length | ||
| # list1.length.times do |i| | ||
| # if list1.include?(list2[i]) | ||
| # result << list2[i] | ||
| # end | ||
| # end | ||
| # return result | ||
| # else | ||
| # list2.length.times do |i| | ||
| # if list2.include?(list1[i]) | ||
| # result << list1[i] | ||
| # end | ||
| # end | ||
| # return result | ||
| # end | ||
| # end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,30 @@ | ||
| #Time complexity: O(n+m+l) | ||
| #Space complexity: O(n^2) | ||
|
|
||
| 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. The time complexity is O(n^2) because You seem to be trying to check to see if the string is a palindrome. The problem states that you are checking to see if the letters could be re-arranged into a palindrome.
Author
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. Ah...I went back to see the problem. I misunderstood the problem. My mind was thinking about the palindrome that I did a few weeks ago. |
||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||
| return true if string == "" | ||
|
|
||
| #Convert the string to an array of letters | ||
| string_array = string.chars | ||
|
|
||
| #reverse_string_array | ||
| reverse_string_array = [] | ||
| string_array.each do |letter| | ||
| reverse_string_array.unshift(letter) | ||
| end | ||
|
|
||
| #create a string_hash by having each element string_array as keys | ||
| string_hash = {} | ||
|
|
||
| string_array.length.times do |i| | ||
| string_hash[string_array[i]] = reverse_string_array[i] | ||
| end | ||
|
|
||
| #compare each key and value of the hash | ||
| string_hash.each do |key, value| | ||
| if key == value | ||
| return true | ||
| end | ||
| end | ||
| return false | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,23 @@ | ||
|
|
||
| #Time complexity: O(n+m) | ||
| #Space complexity: O(n) | ||
| def permutations?(string1, string2) | ||
|
Comment on lines
+1
to
3
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 will fail for |
||
| raise NotImplementedError, "permutations? not implemented" | ||
| return false if (string1.length != string2.length) | ||
|
|
||
| #convert both strings to arrays of letters | ||
| array1 = string1.chars | ||
| array2 = string2.chars | ||
|
|
||
| #create a hash by assigning each element of array1 for the keys of hash | ||
|
|
||
| hash = {} | ||
| array1.each do |letter| | ||
| hash[letter] = true | ||
| end | ||
|
|
||
| array2.each do |letter| | ||
| if hash[letter] != 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.
👍