Skip to content

Commit 91da850

Browse files
authored
feat: valid-anagram
1 parent 2eb3c90 commit 91da850

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

valid-anagram/hozzijeong.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
/**
8+
* map을 통해 문자의 개수를 저장하고, 두 문자열의 문자 개수를 비교해서 다른 값이 있다면 false를 반환하도록 풀이를작성했습니다
9+
*
10+
*/
11+
var isAnagram = function(s, t) {
12+
if(s.length !== t.length) return false;
13+
14+
const length = s.length;
15+
16+
const stringMap = new Map();
17+
const targetMap = new Map();
18+
19+
for(let i = 0; i < length; i ++){
20+
21+
const stringChar = s[i];
22+
const currentStringMap = stringMap.get(stringChar);
23+
24+
if(currentStringMap){
25+
stringMap.set(stringChar, currentStringMap + 1)
26+
}else{
27+
stringMap.set(stringChar, 1)
28+
}
29+
30+
const targetChar = t[i];
31+
const currentTargetMap = targetMap.get(targetChar);
32+
33+
if(currentTargetMap){
34+
targetMap.set(targetChar, currentTargetMap + 1)
35+
}else{
36+
targetMap.set(targetChar, 1)
37+
}
38+
}
39+
40+
for(const [char, count] of [...stringMap]){
41+
const targetCount = targetMap.get(char);
42+
43+
if(!targetCount) return false;
44+
if(targetCount !== count) return false;
45+
}
46+
47+
return true
48+
};

0 commit comments

Comments
 (0)