Find the K-Beauty of a Number — LeetCode 2269 Python Solution
EasyMathStringSliding Window
- Problem
- #2269
- Pattern
- Sliding Window
- Reading time
- 2 min
- Source
- leetcode.com
The problem
The k-beauty of an integer num is defined as the number of substrings of num when it is read as a string that meet the following conditions: It has a length of k. It is a divisor of num.
Example
- Input
- num = 240, k = 2
- Output
- 2
- Explanation
- The following are the substrings of num of length k:
Python solution
Python
class Solution:
def divisorSubstrings(self, num: int, k: int) -> int:
ans = 0
s = str(num)
for i in range(len(s) - k + 1):
t = int(s[i : i + k])
if t and num % t == 0:
ans += 1
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(\log num \times k) |
| Space | O(\log num + k) auxiliary |
Pattern: Sliding Window
Collapse a nested loop over every subarray into a single pass with two indices. LeetCode 2269. Find the K-Beauty of a Number is filed here because LeetCode tags it Sliding Window, which is the vocabulary this hub collects.
The sliding window guide has the Python template for the pattern and the 116 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 2269. Find the K-Beauty of a Number?
- LeetCode 2269. Find the K-Beauty of a Number is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2269. Find the K-Beauty of a Number?
- The Python solution on this page runs in O(\log num \times k).
- What is the space complexity of LeetCode 2269. Find the K-Beauty of a Number?
- The Python solution on this page uses O(\log num + k) auxiliary space.
- What topics does LeetCode 2269. Find the K-Beauty of a Number cover?
- LeetCode 2269. Find the K-Beauty of a Number is tagged Math, String and Sliding Window on LeetCode.