Skip to content

Commit b34009a

Browse files
committed
add solution : 242. Valid Anagram
1 parent d62602e commit b34009a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

โ€Žvalid-anagram/mmyeon.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*
3+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ• :
4+
* - ๋‘ ๋ฌธ์ž ์ •๋ ฌํ•˜๋ฉด O(nlogn)์ด๋‹ˆ๊นŒ ์ •๋ ฌ ๋Œ€์‹  ๊ฐ์ฒด ์‚ฌ์šฉํ•ด์„œ ๋นˆ๋„์ˆ˜ ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์„ ํƒ
5+
* - ์ฒซ ๋ฒˆ์จฐ ๋ฌธ์ž์—ด ์ˆœํšŒํ•ด์„œ ๊ฐ์ฒด์— ๋ฌธ์ž๋ณ„ ๋นˆ๋„์ˆ˜ ์ €์žฅํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด ์ˆœํšŒํ•˜๋ฉด์„œ ๋นˆ๋„์ˆ˜ ๊ฐ์†Œ์‹œํ‚ค๊ธฐ
6+
* - ๋ชจ๋“  ๋ฌธ์ž์˜ ๋นˆ๋„์ˆ˜๊ฐ€ 0์ด ๋˜์–ด์•ผ anagram์ด๋ผ๋Š” ์˜๋ฏธ๋‹ˆ๊นŒ, 0์ธ ๊ฒฝ์šฐ true ๋ฆฌํ„ด
7+
*
8+
* ์‹œ๊ฐ„๋ณต์žก๋„ :
9+
* - ๋‘ ๊ฐ์ฒด for๋ฌธ์œผ๋กœ ์ˆœํšŒํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ O(n)
10+
*
11+
* ๊ณต๊ฐ„๋ณต์žก๋„ :
12+
* - ๋ฌธ์ž ๋นˆ๋„์ˆ˜๋ฅผ ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋Š” ์ž…๋ ฅ ๋ฌธ์ž์—ด ๊ธธ์ด์— ๋น„๋ ˆํ•˜๋‹ˆ๊นŒ O(n)
13+
*
14+
*/
15+
16+
function isAnagram(s: string, t: string): boolean {
17+
// ๋‘ ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋Š” anagram์ด ๋  ์ˆ˜ ์—†์œผ๋‹ˆ๊นŒ ์ดˆ๊ธฐ ๋ฆฌํ„ด ์ฒ˜๋ฆฌ
18+
if (s.length !== t.length) return false;
19+
20+
const charCount: Record<string, number> = {};
21+
22+
for (const letter of s) {
23+
charCount[letter] = (charCount[letter] ?? 0) + 1;
24+
}
25+
26+
for (const letter of t) {
27+
if (!charCount[letter]) return false;
28+
charCount[letter]--;
29+
}
30+
31+
for (const count in charCount) {
32+
if (charCount[count] !== 0) return false;
33+
}
34+
35+
return true;
36+
}

0 commit comments

Comments
ย (0)