If the length of the two strings is different, then GG is used to determine whether the characters are equal. One idea is to sort them first, and then determine whether the two strings are equal, because if the two strings are alphabetic, Sort () : sort() : sort()) : sort() : sort()) : sort() : sort()) : sort() : sort()) So normally we sorted the need to write a function, in the sort () the parameters of the position, so that the sorting sorted according to our own ideas, and of course in this topic, no matter how to sort is doesn’t matter, because even row, as number two strings character under the condition of random row of the result is the same, So that’s the easiest thing to do.

var isAnagram = function(s, t) { 
    return s.split(' ').sort().join(' ') === t.split(' ').sort().join(' ');
};
Copy the code

The second way to create an Object, without sorting, is to define an Object. If the two characters are the same, add one to one and subtract one from the other, and each Object should theoretically have the value 0. The other is the way to determine if each of them is 0, arr. Every (v =>! V), every returns a Boolean, that is, if every item matches the criteria, then true, otherwise false. And v = >! V can determine if the number is 0 or not, otherwise it will be false.

var isAnagram = function(s, t) {
    if(s.length ! == t.length)return false;
    let h = {};
    for (var i = s.length - 1; i>=0; i--) { h[s[i]] = (h[s[i]] ||0) + 1;
        h[t[i]] = (h[t[i]] || 0) -1;
    }
    return Object.values(h).every(v= >! v); };Copy the code