Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bug Report for duplicate-integer #3971

Closed
@TishinIlia

Description

@TishinIlia

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp