DEV Community

Discussion on: A classic interview question

Collapse
3zzy profile image
Ibrahim Ezzy

aaaaa and abbbb is not an anagram in the first place, is it?

Thread Thread
sargalias profile image
Spyros Argalias

That's right. That's what we're testing for: whether they are anagrams of each other. So in the str1 = aaaaa and str2 = abbbb case it should return false because as you say they're not anagrams of each other.

Thread Thread
3zzy profile image
Ibrahim Ezzy

function anagram(str1, str2) {
return (
str1.length == str2.length &&
str1.split("").every(c => str2.includes(c)) &&
str2.split("").every(c => str1.includes(c))
);
}

... and its still about 70% faster.

Thread Thread
sargalias profile image
Spyros Argalias

Nice :)

Thread Thread
denispostu profile image
DenisPostu

The new method returns true for "abb", "aab", I don't think it should.

Thread Thread
3zzy profile image
Ibrahim Ezzy

You're right, every just won't work. My method works for a few cases but not all.