-
프로그래머스 / 크기가 작은 부분 문자열코딩 테스트/Level 1 2022. 12. 24. 12:41반응형
https://school.programmers.co.kr/learn/courses/30/lessons/147355
파이썬
solution = lambda t, p: sum(1 for i in range(len(t) - len(p) + 1) if int(t[i: i + len(p)]) <= int(p))
def solution(t, p): length = len(p) p = int(p) answer = 0 for i in range(len(t) - length + 1): if int(t[i: i + length]) <= p: answer += 1 return answer
코틀린
class Solution { fun solution(t: String, p: String): Int { var answer = 0 val num = p.toInt() for (i in 0..(t.length - p.length)) { val temp = t.slice(i until (i + p.length)) if (temp.toInt() <= num) answer++ } return answer } }
class Solution { fun solution(t: String, p: String): Int { val num = p.toInt() return (0..(t.length - p.length)) .map { t.slice(it until (it + p.length)) } .count { it.toInt() <= num } } }
반응형