Skip to content

Latest commit

 

History

History
43 lines (35 loc) · 825 Bytes

File metadata and controls

43 lines (35 loc) · 825 Bytes

String

Screen Shot 2023-02-04 at 5 05 34 PM

Map

T.C O(n) S.C O(1)

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function(s, t) {
    let map = new Map();
    for(let i = 0; i < s.length; i++) {
        if(map.has(s[i])) {
            map.set(s[i], map.get(s[i]) + 1);
        } else {
            map.set(s[i], 1);
        }
    }

    for(let j = 0; j < t.length; j++) {
        if(map.has(t[j])) {
            map.set(t[j], map.get(t[j]) - 1);
        } else {
            map.set(t[j], 1);
        }
    }

    for(let [key, value] of map) {
        if(value > 0) {
            return false;
        }
    }

    return true;
};