Journal Archive

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function(s, t) {
    if (s.length !== t.length) return false;
    const hash = {};
    for (let i = 0; i < s.length; i++) {
        if (!hash[s[i]]) {
            hash[s[i]] = 1;
        } else {
            hash[s[i]] += 1;
        }
        
        if (!hash[t[i]]) {
            hash[t[i]] = -1;
        } else {
            hash[t[i]] -= 1;
        }
    }
    
    return Object.values(hash).every( count => count === 0);
};

Day 69: Solving one of LeetCode problems

242. Valid Anagram Difficulty - Easy

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

 

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
		

Example 2:

Input: s = "rat", t = "car"
Output: false