Skip to content

Commit c669b7b

Browse files
committed
πŸ“ Docs: solved1 - valid-anagram
1 parent 05ffb72 commit c669b7b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

β€Žvalid-anagram/jangwonyoon.jsβ€Ž

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
/**
8+
* μœ λ‹ˆν¬ν•œ κ°’λ§Œ ν™•μΈν•œλ‹€.
9+
* 1. core: Map μ‚¬μš©ν•˜κ³ , λͺ¨λ“  string을 ν• λ‹Ήν•œλ‹€.
10+
* 2. string을 μˆœνšŒν•΄μ„œ ν• λ‹Ήν•œλ‹€.
11+
* 3. map에 μš”μ†Œκ°€ 있으면 있으면 + 1, μ—†μœΌλ©΄ 1
12+
* 4. map을 μˆœνšŒν•΄μ„œ μš”μ†Œκ°€ μžˆλ‹€λ©΄ -1을 ν•΄μ€€λ‹€.
13+
* 5. HashMap을 λ‹€μ‹œ ν•œλ²ˆ μˆœνšŒν•΄μ„œ, 1이상 값이 μžˆλ‹€λ©΄ μœ λ‹ˆν¬ν•œ 값이 μ•„λ‹ˆκΈ° λ•Œλ¬Έμ— false, HashMap의 λͺ¨λ“  값듀이 0이면 true
14+
15+
* κ³΅κ°„λ³΅μž‘λ„ O(N)
16+
* μ‹œκ°„λ³΅μž‘λ„ O(N)
17+
*/
18+
19+
20+
var isAnagram = function(s, t) {
21+
const hashMap = new Map();
22+
23+
// μ˜ˆμ™Έ 처리
24+
if (s.length !== t.length) {
25+
return false;
26+
}
27+
28+
29+
for (const string of s) {
30+
if (!hashMap.has(string)) {
31+
hashMap.set(string, 1)
32+
} else {
33+
hashMap.set(string, hashMap.get(string) + 1)
34+
}
35+
}
36+
37+
for (const string of t) {
38+
if (hashMap.has(string)) {
39+
hashMap.set(string, hashMap.get(string) - 1);
40+
}
41+
}
42+
43+
// 0이 μ•„λ‹Œ 값이 μžˆλŠ”κ²½μš° - false
44+
for (const [key , value] of hashMap) {
45+
// early return
46+
if (value > 0) return false;
47+
}
48+
49+
return true;
50+
};

0 commit comments

Comments
Β (0)