2269. Find the K-Beauty of a Number

2023. 1. 26. 22:35Algorithm/Leetcode, Lintcode, HackerRank, etc.

    목차
반응형

Kotlin

class Solution {
    fun divisorSubstrings(num: Int, k: Int): Int {
        var s: String = num.toString()
        if (s.length < k) {
            return 0
        }

        var cnt: Int = 0

        for (i in 0 .. s.length - k) {
            var part = s.slice(i until i + k)
            var intval = part.toInt()

            if (intval == 0) {
                continue
            }

            if (num % intval == 0) {
                cnt += 1
            }
        }

        return cnt
    }
}

Python

class Solution:
    def divisorSubstrings(self, num: int, k: int) -> int:
        n = len(str(num))
        if n < k:
            return 0

        cnt = 0
        s = str(num)

        for i in range(0, n - k + 1):
            part = int(s[i:i + k])
            if 0 == part:
                continue

            if num % part == 0:
                cnt += 1

        return cnt
반응형