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

Commit971a5a7

Browse files
authored
Update 242-Valid-Anagram.js
we should we hasOwnproperty because for value zero case if condition fails
1 parente815ef4 commit971a5a7

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

‎javascript/242-Valid-Anagram.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*@return {boolean}
55
*/
66
varisAnagram=function(s,t){
7+
8+
//solution 1
79
letmap={};
810

911
if(s.length!==t.length){
@@ -28,7 +30,28 @@ var isAnagram = function(s, t) {
2830

2931
returntrue;
3032

31-
32-
33-
33+
//solution 2
34+
if(s.length!==t.length){
35+
returnfalse;
36+
}
37+
letsMap={};
38+
lettMap={};
39+
for(leti=0;i<s.length;i++){
40+
if(sMap.hasOwnProperty(s[i])){
41+
sMap[s[i]]++
42+
}else{
43+
sMap[s[i]]=1;
44+
}
45+
if(tMap.hasOwnProperty(t[i])){
46+
tMap[t[i]]++
47+
}else{
48+
tMap[t[i]]=1;
49+
}
50+
}
51+
for(letkinsMap){
52+
if(sMap[k]!==tMap[k]){
53+
returnfalse;
54+
}
55+
}
56+
returntrue;
3457
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp