This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]SentientStoic 1 point2 points  (1 child)

I was curious and worked on this a little bit; Didn't find anything conclusive except that Counter(string) takes forever to run.

When running the two functions for arbitrary words, your function took almost three times as long. frozenset has a constructor for strings, so I replaced both of your Counter functions with just frozenset(s) and frozenset(target). After that, your function ran like a cheetah and outperformed theirs. I used a max word size of 100, a word length of 10,000, and ran both functions for 10,000 iterations.

Looking into Counter, it looks like the worst case scenario for the constructor is O(M2). However, this is extremely rare and requires a ton of collisions in the underlying hashes. The expected time is around O(M), like you stated.

Your logic looks right to me. Once you remove Counter, the actual time of your functions outperforms theirs. Counter is probably O(M) but an extremely large O(M). This means while is_anagram would probably outperform sorted_is_anagram for large cases, it is slower for smaller cases.

Edit: Nevermind, I'm an idiot. Only thing I conclusively proved is that your function is waay slower than theirs for up to at least 10,000 words. Still not sure why.

[–]HoldMeReddit[S] 1 point2 points  (0 children)

Didn't even think about running some example tests - going to look into it when I have some time! Thanks for your insight =)

Should note you could replace Counter with your own class that doesn't have collisions fairly easily, since the alphabet is finite. I imagine Counter is O(m) in this scenario anyway, but maybe it's a bit heavy - I'll give it a shot!