- Notifications
You must be signed in to change notification settings - Fork2.4k
Closed
Description
Bug Report forhttps://neetcode.io/problems/duplicate-integer
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.?title=Bug Report for is-anagram
Hi team, hash-map solution for JS doesn't cover scenario, where key length is equal, but some keys have different values
classSolution{/** *@param {string} s *@param {string} t *@return {boolean} */isAnagram(s,t){if(s.length!==t.length){returnfalse;}constcountS={};constcountT={};for(leti=0;i<s.length;i++){countS[s[i]]=(countS[s[i]]||0)+1;countT[t[i]]=(countT[t[i]]||0)+1;}for(constkeyincountS){if(**countS[key]**!==**countT[key]**){returnfalse;}}returntrue;}
e.g.
s="raceear" // {'r':2, 'a':2, 'c':1,'e':2}
t="carrace" // {'c':2, 'a':2, 'r': 2, 'e':1}
number of keys is the same, keys are same, but values ofr andc are different
my not optimized solution is
isAnagram(s,t){if(s.length!==t.length){returnfalse}constdict1=s.split('').reduce((acc,char)=>{if(acc[char]){acc[char]+=1}else{acc[char]=1}returnacc},{})constdict2=t.split('').reduce((acc,char)=>{if(acc[char]){acc[char]+=1}else{acc[char]=1}returnacc},{})if(Object.keys(dict1).length!==Object.keys(dict2).length){returnfalse}for(const[entrySKey,entrySValue]ofObject.entries(dict1)){consta=Object.entries(dict2)constentryT=a.find(([entryTKey,_])=>{console.log('entryTKey',entryTKey)returnentryTKey===entrySKey})if(!entryT){returnfalse}if(entryT[1]!==entrySValue){returnfalse}}returntrue}
Metadata
Metadata
Assignees
Labels
No labels