Skip to content

Commit 0165731

Browse files
committed
solution of 242 valid anag
ram
1 parent 6b3390e commit 0165731

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @description ๋ฌธ์ž์˜ ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ์™„์ „ํžˆ ๊ฐ™์€ ์• ๋„ˆ๊ทธ๋žจ์ธ์ง€ ํŒ๋ณ„
3+
* @param {string} s ๋ฌธ์ž์—ด 1
4+
* @param {string} t ๋ฌธ์ž์—ด 2
5+
* @returns {boolean} ์• ๋„ˆ๊ทธ๋žจ ์—ฌ๋ถ€
6+
*/
7+
function isAnagram(s: string, t: string): boolean {
8+
// ๋ฌธ์ž์—ด๋“ค์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด ์• ๋„ˆ๊ทธ๋žจ ์•„๋‹˜
9+
if (s.length !== t.length) return false;
10+
11+
// ์˜์–ด ์†Œ๋ฌธ์ž ๊ฐœ์ˆ˜ "a"~"z" = 26๊ฐœ
12+
const LOWER_ALPHABET_COUNT = 26;
13+
// ์•ŒํŒŒ๋ฒณ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด
14+
// index 0 -> 'a'
15+
// index 1 -> 'b'
16+
const count = new Array(LOWER_ALPHABET_COUNT).fill(0);
17+
// a์˜ ์•„์Šคํ‚ค์ฝ”๋“œ
18+
const BASE = "a".charCodeAt(0);
19+
20+
for (let i = 0; i < s.length; i++) {
21+
count[s.charCodeAt(i) - BASE]++;
22+
count[t.charCodeAt(i) - BASE]--;
23+
}
24+
25+
// ๋ชจ๋“  ๋ฌธ์ž์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ 0์ด๋ฉด
26+
// s์™€ t์˜ ๋ฌธ์ž ์ข…๋ฅ˜์™€ ๊ฐœ์ˆ˜๊ฐ€ ์™„์ „ํžˆ ๋™์ผ
27+
return count.every((value) => value === 0);
28+
}

0 commit comments

Comments
ย (0)