-
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?
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,4 +1,5 @@ | ||||||
using System.Collections.Generic; | ||||||
using System.Linq; | ||||||
|
||||||
namespace Code | ||||||
{ | ||||||
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
public static bool AreAllCharactersUniqueLinQSolution(string input) | ||||||
{ | ||||||
if (string.IsNullOrEmpty(input)) | ||||||
{ | ||||||
return true; | ||||||
} | ||||||
|
||||||
return input.Distinct().ToArray().Length == input.Length; | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,5 +77,30 @@ public static bool AreStringsPermutationNoSort(string string1, string string2) | |
|
||
return true; | ||
} | ||
|
||
// Space: O(N) | ||
// Time: O(N) | ||
public static bool AreStringPermutationsIntValues(string string1, string string2) | ||
{ | ||
if (string.IsNullOrEmpty(string1) || string.IsNullOrEmpty(string2)) | ||
{ | ||
throw new ArgumentException("Input strings cannot be null or empty"); | ||
} | ||
|
||
if (string1.Length != string2.Length) | ||
{ | ||
return false; | ||
} | ||
|
||
int summyString1 = 0; | ||
int summyString2 = 0; | ||
for (int i = 0; i < string1.Length; i++) | ||
{ | ||
summyString1 = summyString1 + string1[i]; | ||
summyString2 = summyString2 + string2[i]; | ||
} | ||
|
||
return summyString1 == summyString2; | ||
} | ||
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 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: The string ACC will have the following hash code: The string ABD will have the following hash code: These strings are going to be considered permutations using this algorithm, but in reality they're not permutations. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,5 +50,39 @@ public static void ReplaceSpaces(char[] inputString, int length) | |
} | ||
} | ||
} | ||
|
||
// 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 commentThe 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. |
||
{ | ||
if (inputString == null) | ||
{ | ||
throw new ArgumentNullException(nameof(inputString), "Value cannot be null"); | ||
} | ||
|
||
char[] outputString = new char[inputString.Length * 3]; | ||
int outputIndex = 0; | ||
for (int i = 0; i < inputString.Length; i++) | ||
{ | ||
if (char.IsWhiteSpace(inputString[i])) | ||
{ | ||
outputString[outputIndex++] = '%'; | ||
outputString[outputIndex++] = '2'; | ||
outputString[outputIndex++] = '0'; | ||
} | ||
else | ||
{ | ||
outputString[outputIndex++] = inputString[i]; | ||
} | ||
} | ||
|
||
char[] finalString = new char[outputIndex]; | ||
for (int i = 0; i < outputIndex; i++) | ||
{ | ||
finalString[i] = outputString[i]; | ||
} | ||
|
||
return new string(finalString); | ||
} | ||
} | ||
} |
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:
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