Time Complexity: $$O(NK \log (K) )$$, where $$N$$ is the length of strs, and $$K$$ is the maximum length of a string in strs. The outer loop has complexity $$O(N)$$ as we iterate through each string. Then, we sort each string in $$O(K \log K)$$ time.

Space Complexity: $$O(N*K)$$, the total information content stored in ans.

Approach #2: Categorize by Count [Accepted]

Intuition

Two strings are anagrams if and only if their character counts (respective number of occurrences of each character) are the same.

Algorithm

We can transform each string s into a character count count, consisting of 26 non-negative integers representing the number of 'a''s, 'b''s, 'c''s, etc. We use these counts as the basis for our hash map.

In Java, the hashable representation of our count will be a string delimited with '#' characters. For example, "abbccc" will be "#1#2#3#0#0#0...#0" where there are 26 entries total. In python, the representation will be a tuple of the counts. For example, "abbccc" will be "(1, 2, 3, 0, 0, ..., 0)", where again there are 26 entries total.

Time Complexity: $$O(N * K)$$, where $$N$$ is the length of strs, and $$K$$ is the maximum length of a string in strs. Counting each string is linear in the size of the string, and we count every string.

Space Complexity: $$O(N*K)$$, the total information content stored in ans.

Your second Java "solution" is bad. It for example claims that "a" and "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" are anagrams of each other (for input ["a", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"] it returns [["a","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]]).

@awice Well, even if that hashing algorithm weren't so simple, it would still only give you an int and thus likely collisions after only about 65536 strings (birthday paradox). And since 267 > 232, I could also definitely find two different strings just seven letters long which have the same hash code.

Your new version is about equally easy to break, for example you now fail this input:
["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"]

Your key for both is "9719899100101102103104105106107108109110111112113114115116117118119120121122". For the "a"-string it starts with "971"+"98" and for the "b"-string it starts with "97" + "198".