Valid Anagram

medium

By - Aman Pareek

Last Updated - 21/09/2024

Problem Statement

Given two strings, string1 and string2, determine if string2 is an anagram of string1. 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: str1 = "listen" , str2 = "silent"

Output: true

Solution 1: Sorting Method

function isAnagramSorting(s, t) {
    return s.split('').sort().join('') === t.split('').sort().join('');
} 

const str11 = "listen";
const str21 = "silent";
isAnagramSorting(str11,str21);  //output: true 

Solution 2: Frequency Count with Object

function isAnagramFrequencyObject(s, t) {
    if (s.length !== t.length) return false;

    const count = {};
    for (const char of s) count[char] = (count[char] || 0) + 1;
    for (const char of t) {
        if (!count[char]) return false;
        count[char]--;
        if (count[char] < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramFrequencyObject(str11,str21);  //output: true 

Solution 3: Frequency Count with Array

function isAnagramFrequencyArray(s, t) {
    if (s.length !== t.length) return false;

    const count = new Array(26).fill(0);
    const base = 'a'.charCodeAt(0);
    for (const char of s) count[char.charCodeAt(0) - base]++;
    for (const char of t) {
        if (--count[char.charCodeAt(0) - base] < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramFrequencyArray(str11,str21);  //output: true 

Solution 4: Using Map

function isAnagramMap(s, t) {
    if (s.length !== t.length) return false;

    const count = new Map();
    for (const char of s) count.set(char, (count.get(char) || 0) + 1);
    for (const char of t) {
        if (!count.has(char)) return false;
        count.set(char, count.get(char) - 1);
        if (count.get(char) < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramMap(str11,str21);  //output: true 

Solution 5: Using Set for Character Comparison

function isAnagramSet(s, t) {
    if (s.length !== t.length) return false;
    
    const sSet = new Set(s);
    const tSet = new Set(t);

    if (sSet.size !== tSet.size) return false;

    for (const char of sSet) {
        if (!tSet.has(char)) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramSet(str11,str21);  //output: true 

Solution 6: Character Count with reduce

function isAnagramReduce(s, t) {
    if (s.length !== t.length) return false;

    const count = s.split('').reduce((acc, char) => {
        acc[char] = (acc[char] || 0) + 1;
        return acc;
    }, {});

    for (const char of t) {
        if (!count[char]) return false;
        count[char]--;
        if (count[char] < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramReduce(str11,str21);  //output: true 

Solution 7: Using charCodeAt for Frequency Counting

function isAnagramCharCode(s, t) {
    if (s.length !== t.length) return false;

    const count = new Array(26).fill(0);
    const base = 'a'.charCodeAt(0);

    for (const char of s) count[char.charCodeAt(0) - base]++;
    for (const char of t) {
        if (--count[char.charCodeAt(0) - base] < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramCharCode(str11,str21);  //output: true 

Solution 8: Using Array.prototype.some for Validation

function isAnagramSome(s, t) {
    if (s.length !== t.length) return false;

    const sCount = Array.from(s).reduce((acc, char) => {
        acc[char] = (acc[char] || 0) + 1;
        return acc;
    }, {});
    return !Array.from(t).some(char => {
        if (!sCount[char]) return true;
        sCount[char]--;
        return sCount[char] < 0;
    });
} 

const str11 = "listen";
const str21 = "silent";
isAnagramSome(str11,str21);  //output: true 

Solution 9: Using for...of Loop with Counter

function isAnagramCounter(s, t) {
    if (s.length !== t.length) return false;

    const count = {};
    for (const char of s) count[char] = (count[char] || 0) + 1;
    for (const char of t) {
        if (!count[char]) return false;
        count[char]--;
        if (count[char] < 0) return false;
    }
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramCounter(str11,str21);  //output: true 

Solution 10: Using String.prototype.split and join

function isAnagramSplitJoin(s, t) {
    if (s.length !== t.length) return false;

    const count = Array(26).fill(0);
    const base = 'a'.charCodeAt(0);
    
    s.split('').forEach(char => count[char.charCodeAt(0) - base]++);
    t.split('').forEach(char => {
        if (--count[char.charCodeAt(0) - base] < 0) return false;
    });
    return true;
} 

const str11 = "listen";
const str21 = "silent";
isAnagramSplitJoin(str11,str21);  //output: true 

Popular Solutions