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
interviews/company/facebook/GroupAnagrams.java #91
Comments
I don't think there is any reason for using the first sort. It would anyhow group together using just one sort method in inner loop. Coming to your approach, how sure are you that there would be no hash collisions? |
yes no need to sort. collisions ? a solid hash function. but since this is an interview question - the interviewer (depending on their degree of analness) may not dig you for that. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The given solution for this can be improved by not having to sort at all.
There are 2 calls to Arrays.sort().... This can become expensive if the program input array is large over 10K. A faster solution would just be to compute the hash of each value using a prime number assigned to each letter from a-z ( can be quckly generate in code). Then for each value in the array compute hash and store it inside a map. Then to get all the grouped anagrams you lookup the map or insert whatever the case maybe. I can provide code...too
The text was updated successfully, but these errors were encountered: