2269. Find the K-Beauty of a Number
2023. 1. 26. 22:35ㆍAlgorithm/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
반응형
'Algorithm > Leetcode, Lintcode, HackerRank, etc.' 카테고리의 다른 글
2544. Alternating Digit Sum (0) | 2023.01.28 |
---|---|
2255. Count Prefixes of a Given String (0) | 2023.01.26 |
2389. Longest Subsequence With Limited Sum (0) | 2023.01.24 |
2395. Find Subarrays With Equal Sum (0) | 2023.01.24 |
2405. Optimal Partition of String (0) | 2023.01.24 |