Skip to content

Commit d21dcff

Browse files
committed
climbing-stairs solved
1 parent 01afec6 commit d21dcff

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

โ€Žvalid-anagram/kut7728.swift

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1+
//๋ฌธ์ž์—ด s, t๋ฅผ ๋ฐ›๊ณ , t๊ฐ€ s์˜ ์• ๋„ˆ๊ทธ๋žจ์ด๋ฉด true, ์•„๋‹ˆ๋ฉด false ์ถœ๋ ฅ
2+
13
class Solution {
2-
func climbStairs(_ n: Int) -> Int {
3-
var result = 0
4+
func isAnagram(_ s: String, _ t: String) -> Bool {
5+
///๋‘˜์ด ๋ฌธ์ž ๊ฐฏ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ๋ฐ”๋กœ False
6+
if s.count != t.count { return false }
47

5-
for i in 0...(n/2) {
6-
if i == 0 {
7-
result += 1
8-
continue
9-
}
10-
11-
let x = n - (2 * i)
8+
///ํ•ด์‰ฌํ…Œ์ด๋ธ” ์‚ฌ์šฉ
9+
var wordDic: [Character: Int] = [:]
1210

13-
result += ncm(x+i, i)
11+
///s์˜ ๊ธ€์ž๋“ค์€ 1์”ฉ ์ถ”๊ฐ€, t์˜ ๊ธ€์ž๋“ค์€ 1์”ฉ ๊ฐ์†Œ
12+
for (sChar, tChar) in zip(s, t) {
13+
wordDic[sChar, default: 0] += 1
14+
wordDic[tChar, default: 0] -= 1
1415
}
1516

16-
return result
17-
}
18-
19-
func ncm(_ n: Int, _ m: Int) -> Int {
20-
if m == 1 { return n }
21-
if m == n { return 1 }
22-
return (1...m).reduce(1) { $0 * ($1 + n-m)/$1 }
17+
///๋”•์…”๋„ˆ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ’์ด ์ƒ์‡„๋˜์–ด 0์ด ๋˜๋ฉด true
18+
return wordDic.values.allSatisfy { $0 == 0 }
2319
}
2420
}

0 commit comments

Comments
ย (0)