프로그래머스-크기가 작은 부분문자열

효딩딩·2023년 12월 20일
0

문제

풀이

func solution(_ t:String, _ p:String) -> Int {
    var result = 0
    let pLength = p.count
    let pnum =  Int(p)!
  
    for i in 0...(t.count - p.count) {
        
        let start = t.index(t.startIndex, offsetBy: i)
        let end = t.index(t.startIndex, offsetBy: i + pLength)
        let str = String(t[start..<end])
        
        if Int(str)! <= pnum {
            result += 1
        }
    }
    return result
}

solution("3141592", "271")
profile
어제보다 나은 나의 코딩지식

0개의 댓글