Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 11, 2022 03:10 am GMT

Valid Anagram

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.

var isAnagram = function (s, t) {    if (s.length !== t.length) {        return false;    }    let countS = new Map();    let countT = new Map();    for (let start = 0; start < s.length; start++) {        countS.set(s[start], 1 + (countS.get(s[start]) ? countS.get(s[start]) : 0));        countT.set(t[start], 1 + (countT.get(t[start]) ? countT.get(t[start]) : 0));    }    for (const [key, value] of countS) {        if (countS.get(key) !== countT.get(key)) {            return false;        }    }    return true;}

Time Complexity : O(n)
Space Complexity : O(s+t)


Original Link: https://dev.to/judearasu/valid-anagram-43db

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To