Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

Lvcios
Copy link

@Lvcios Lvcios commented Jan 11, 2022

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

@Lvcios Lvcios changed the title linq solution for chapter 1 exercise 1 chapter 1 exercise 1 and 2 Jan 12, 2022
@Lvcios Lvcios changed the title chapter 1 exercise 1 and 2 chapter 1 exercises 1,2,3 Jan 14, 2022
Copy link
Owner

@robkeim robkeim left a 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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
// Space: O(n)
// Space: O(N)

Copy link
Author

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Time: O(1)
// Time: O(N)

Distinct() requires an enumeration over the entire string so has an O(N) runtime

}

return summyString1 == summyString2;
}
Copy link
Owner

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)
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants