Skip to content

Commit c04e4f6

Browse files
committed
valid-anagram: 같은 로직 시간 단축 풀이 추가
1 parent d10eddd commit c04e4f6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-anagram/naringst.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,29 @@ var isAnagram = function (s, t) {
4646

4747
return isSameDict(sDict, tDict);
4848
};
49+
50+
/**
51+
* 같은 로직인데 53ms 걸린 다른 풀이
52+
*/
53+
54+
var isAnagram = function (s, t) {
55+
if (s.length !== t.length) {
56+
return false;
57+
}
58+
59+
const countA = {};
60+
const countB = {};
61+
62+
for (let i = 0; i < s.length; i++) {
63+
countA[s[i]] = 1 + (countA[s[i]] || 0);
64+
countB[t[i]] = 1 + (countB[t[i]] || 0);
65+
}
66+
67+
for (const key in countA) {
68+
if (countA[key] !== countB[key]) {
69+
return false;
70+
}
71+
}
72+
73+
return true;
74+
};

0 commit comments

Comments
 (0)