Anagram

Anagram

·

1 min read

what is Anagram ?

A word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp.

solution :

Hash Map :

const contacts = new Map();
contacts.set("Jessie", { phone: "213-555-1234", address: "123 N 1st Ave" });
contacts.has("Jessie"); // true
contacts.get("Hilary"); // undefined
contacts.set("Hilary", { phone: "617-555-4321", address: "321 S 2nd St" });
contacts.get("Jessie"); // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete("Raymond"); // false
contacts.delete("Jessie"); // true
console.log(contacts.size); // 1
var isAnagram = function (s, t) {
    if(s.length!==t.length) return false;
    let frequenctCount = new Map();
    for (const char of s) {
        let isElementExist = frequenctCount.get(char);
        isElementExist ? frequenctCount.set(char, isElementExist + 1) : frequenctCount.set(char, 1)
    }
    for (const char of t) {
        let isElementExist = frequenctCount.get(char);
        isElementExist && frequenctCount.set(char, isElementExist - 1)
    }
    // check if all are zero
    for (const [char, count] of frequenctCount) {
        if (count !== 0) return false;
    }

    return true;
};