Share my javascript solution 112ms


  • 0
    M

    var isAnagram = function(s, t) {

    //the input s, t is string, so don't need to check if it is null
    if (s.length !== t.length) {
        return false;
    }
    var map = {};
    for (var i = 0; i < s.length; i++) {
        if (map[s[i]] === undefined) {
            map[s[i]] = 1;
        } else {
            map[s[i]] += 1;
        }
    }
    
    for (var j = 0; j < t.length; j++) {
        //if map[t[j]] === undefined or zero
        if (!map[t[j]]) {
            return false;
        }
        map[t[j]]--;
    }
    return true;
    

    };


Log in to reply
 

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