Closed mx-psi closed 4 years ago
I guess ignoring punctuation and spaces counts. Anything weird such as '①' should not be checked against a simple anagram checker to begin with, so I guess it's ok to fail for cases like these. I'd love to see a PR for this sometime soon. :wink:
The is_anagram snippet does not ignore punctuation or special characters. Furthermore, the running time could be improved using a
collections.Counter
.Expected Snippet Behavior
is_anagram("#anagram", "Nag a ram!")
should beTrue
and run in linear timeCurrent Snippet Behavior
is_anagram("#anagram", "Nag a ram!")
isFalse
.In cases where it does not fail it runs in linearithmic time (because of sorting), instead of linear time.
Possible Solution
One may improve the code as follows:
The code counts the alphanumeric characters on each string in their lowercase version and checks if the counters match. There is no need for sorting and the intermediate strings need not be created.
I intend to open a PR for fixing this but I am unsure as to what counts as a special character. In the suggested fix, str.isalnum considers characters like U+2460 '①' to be alphanumeric and thus not a special character. Is that the correct interpretation of "special character"?