나의 풀이
import Foundation
func solution(_ i:Int, _ j:Int, _ k:Int) -> Int {
var result = 0
for x in i...j {
let array = String(x).map { String($0) }
result += array.filter { $0 == String(k) }.count
}
return result
}
- i~j 범위로 반복문 수행
- int -> String Array 변환 후 k String 있는 요소만 필터링 후 카운팅
다른 사람의 풀이
import Foundation
func solution(_ i:Int, _ j:Int, _ k:Int) -> Int {
return (i...j)
.map {
String($0).filter { String($0) == String(k) }.count
}
.reduce(0, +)
}
'개인 공부 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 한 번만 등장한 문자 swift (0) | 2024.06.14 |
---|---|
[프로그래머스] 이진수 더하기 swift (1) | 2024.06.12 |
[프로그래머스] A로 B 만들기 swift (0) | 2024.06.12 |
[프로그래머스] 중복된 문자 제거 swift (0) | 2024.06.12 |
[프로그래머스] 삼각형의 완성조건 (1) swift (0) | 2024.06.12 |