EasyNo. 2269

Find the K-Beauty of a Number — LeetCode 2269 Python Solution

Topics
Math, String, Sliding Window
Reading time
2 min

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

PythonSolution
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 ans

Complexity

Time
O(log num × k)
Space · auxiliary
O(log num + k)

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

Related problems
No.ProblemDifficulty
0043Multiply StringsMedium
0168Excel Sheet Column TitleEasy
0171Excel Sheet Column NumberEasy
0273Integer to English WordsHard
0412Fizz BuzzEasy
0415Add StringsEasy

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 × 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.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.