Javascript Solution


  • 0
    var isAnagram = function(s, t) {
        if(s.length != t.length) return false;
        
        var hash = s.split('').reduce(function(p, c) {
            p[c] = p[c] ? ++(p[c]) : 1;
            return p;
        }, {});
        
        for(var i = (t.length -1); i >= 0; i--) {
            var c = t.charAt(i);
            if(hash[c]) {
               hash[c]--;
               if(hash[c] < 0) return false;
            } else {
                return false;
            }
        }
        return true;
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.