File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_study
2
+
3
+ /* *
4
+ * ๋ฌธ์ฅ์ ๋ฌธ์ ์์๋ฅผ ๋ฐ๊พธ์ด ์๋ก์ด ๋จ์ด๋ ๋ฌธ์ฅ์ ๋ง๋ค ์ ์๋์ง ํ๋ณํ๋ ๋ฌธ์
5
+ * ์๊ฐ ๋ณต์ก๋: O(n)
6
+ * -> ์ฃผ์ด์ง ๋ฌธ์์ด์ ์ํํ๋ฉฐ Map ์๋ฃ๊ตฌ์กฐ์ ๊ฐ์ ์ฑ์๋ฃ๋ ๊ณผ์ : O(n)
7
+ * -> ์ํ๋ฒณ ๋ฌธ์์ด ์ธํ
๊ณผ์ : O(1)
8
+ * -> Map<์ํ๋ฒณ, ๋น๋> ์ด๊ธฐํ ๊ณผ์ : O(1)
9
+ * -> ์ํ๋ฒณ ๋น๊ต ๊ณผ์ : O(1)
10
+ * O(1) + O(1) + O(1) + O(n) => O(n)
11
+ *
12
+ * ๊ณต๊ฐ ๋ณต์ก๋: O(1)
13
+ * -> ์ํ๋ฒณ ๋น๋์๋ฅผ ์ ์ฅ Map: O(1)
14
+ */
15
+ fun isAnagram (s : String , t : String ): Boolean {
16
+ val alphaArray = CharArray (26 ) { ' a' + it}
17
+
18
+ if (s.length != t.length) return false
19
+
20
+ val sMap = alphaArray.associateWith { 0 }.toMutableMap()
21
+ val tMap = alphaArray.associateWith { 0 }.toMutableMap()
22
+
23
+ for (i in s.indices) {
24
+ sMap[s[i]] = sMap.getValue(s[i]).plus(1 )
25
+ tMap[t[i]] = tMap.getValue(t[i]).plus(1 )
26
+ }
27
+
28
+ for (alphabet in alphaArray) {
29
+ if (sMap[alphabet] != tMap[alphabet]) {
30
+ return false
31
+ }
32
+ }
33
+
34
+ return true
35
+ }
You canโt perform that action at this time.
0 commit comments