-
Notifications
You must be signed in to change notification settings - Fork 13
chapter 1 exercises 1,2,3 #2
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?
Conversation
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.
Thanks so much @Lvcios for your pull request! This is the first time I've had someone create a pull request in one of my projects so that's very exciting 😄
I've added a few comments that you can take a look at/address before merging. Let me know if you have questions!
@@ -49,5 +50,17 @@ public static bool AreAllCharactersUniqueNoAdditionalMemory(string input) | |||
|
|||
return true; | |||
} | |||
|
|||
// Space: O(n) |
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.
Nit:
// Space: O(n) | |
// Space: O(N) |
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.
Hi, I really, really, really, appreciate the feedback. I didn't expect it but it's great to learn from my mistakes.
I'll fix it as soon as I have enough time
@@ -49,5 +50,17 @@ public static bool AreAllCharactersUniqueNoAdditionalMemory(string input) | |||
|
|||
return true; | |||
} | |||
|
|||
// Space: O(n) | |||
// Time: O(1) |
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.
// Time: O(1) | |
// Time: O(N) |
Distinct()
requires an enumeration over the entire string so has an O(N) runtime
} | ||
|
||
return summyString1 == summyString2; | ||
} |
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 is a great idea, but unfortunately, it won't work in all cases :(
What you're doing is effectively creating a hash of the input and comparing to see if they're equal. This opens the opportunity for hash collisions.
Imagine a simplified example where characters have the following values:
A = 1
B = 2
C = 3
D = 4
The string ACC will have the following hash code:
A + C + C = 1 + 3 + 3 = 7
The string ABD will have the following hash code:
A + B + D = 1 + 2 + 4 = 7
These strings are going to be considered permutations using this algorithm, but in reality they're not permutations.
|
||
// Space: O(N) | ||
// Time: O(N) | ||
public static string ReplaceSpaces(char[] inputString) |
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 if we're looking for another string, but in this case the question is asking for us to update the string in the original char array.
Question1_1
I just want to contribute by adding a solution using LinQ. I'm not sure about the time and space complexity but this post can help: What guarantees are there on the run-time complexity (Big-O) of LINQ methods?
Question1_2
Solution by summing character numeric values in each string and comparing if the sum is the same.
Question1_3
Another o(N) solution using 2 arrays but without the real string length