File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ // t๊ฐ s์ ์ฒ ์๋ฅผ ๋ชจ๋ ํฌํจํ๊ณ ์๋ ์ ๋๊ทธ๋จ์ธ์ง๋ฅผ ํ์ธํ๋ ๋ฌธ์
3
+ // ํธ๋ ๋ฐฉ๋ฒ์ 2๊ฐ์ง
4
+ // 1. ์ ๋์ฝ๋๋ก ๋ณํํด์ sort ํด์ ํธ๋ ๋ฐฉ๋ฒ
5
+ // 2. ์ ๋ ฌํ์ง ์๊ณ mutableMap์ผ๋ก ๋งต์ ๋ง๋ค์ด ํธ๋ ๋ฐฉ๋ฒ
6
+ // 2๋ฒ์ผ๋ก ๋ฌธ์ ๋ฅผ ํ์๋ค.
7
+ fun isAnagram (s : String , t : String ): Boolean {
8
+ // ์ฐ์ ์ฒ์๋ถํฐ ๋ String์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ฉด ๋ฐ๋ก false๋ฅผ ๋ฐํ
9
+ if (s.length != t.length) false
10
+
11
+ // s์ ๋ฌธ์์ด์ ์นด์ดํธํ๋ฉด์ ์ ์ฅํ ๊ฐ๋ณ๋ฐฐ์ด์ ์ ์ธํด์ค๋ค.
12
+ val countMap = mutableMapOf<Char , Int >()
13
+
14
+ // s์ ์๋ ๊ฐ๊ฐ์ ๋ฌธ์๋ค์ ๋ํด์ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ ์ถํํ์๋ฅผ ๋งต์ผ๋ก ๊ตฌ์ฑ
15
+ for (c in s){
16
+ countMap[c] = countMap.getOrDefault(c, 0 ) + 1
17
+ }
18
+
19
+ // ์ด์ t์ ๋ฌธ์๋ค์ด ์๊น ๋ง๋ค์ด์ง countMap์ ๋ชจ๋ ์กด์ฌํ๋์ง ํ์ธ
20
+ for (c in t){
21
+ val count = countMap.getOrDefault(c, 0 )
22
+ if (count == 0 ) return false
23
+ countMap[c] = count - 1
24
+ }
25
+ // countMap์ ๋ค์ด์๋ value๊ฐ ์ ๋ถ 0์ด๋ฉด true๋ฅผ ๋ฐํ
26
+ return countMap.values.all { it == 0 }
27
+ }
28
+ }
29
+
You canโt perform that action at this time.
0 commit comments