Skip to content
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

Open
knasim opened this issue Aug 2, 2018 · 2 comments
Open

interviews/company/facebook/GroupAnagrams.java #91

knasim opened this issue Aug 2, 2018 · 2 comments

Comments

@knasim
Copy link

knasim commented Aug 2, 2018

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

@GauravMohla
Copy link

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?

@knasim
Copy link
Author

knasim commented Aug 13, 2018

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants