↓Skip to main content
  1. LeetCode/

LeetCode 2269: Find the K-Beauty of a Number

·2 mins· ·
LeetCode Easy Sliding-Window Math String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-04

  • Submissions: Runtime Error → Accepted

Approach
#

Convert num to a string; each length-k substring is a fixed window. After converting back to an integer:

  • Skip it if it is 0 (0 divides nothing and cannot be used as a divisor).
  • Otherwise check num % sub == 0.

Leading zeros are allowed: int("04") is 4.

My Attempts
#

Attempt 1 ended with return len(res), but res was an integer counter, so len() raised TypeError. return res passed.

Solution
#

class Solution:
    def divisorSubstrings(self, num: int, k: int) -> int:
        s = str(num)
        count = 0
        for right in range(k - 1, len(s)):
            sub = int(s[right - k + 1:right + 1])
            if sub and num % sub == 0:             # 0 is never a divisor
                count += 1
        return count

num has at most 10 digits, so converting each slice is negligible; for strict O(n), roll the value with sub = sub * 10 + new_digit - old_digit * 10^k.

Complexity
#

Time O(n · k) with n digits, space O(n).

Mistakes to Avoid
#

  • Dividing by a substring equal to 0: ZeroDivisionError.
  • Calling len() on an integer.

Interview Explanation
#

I turn the number into a string, convert every length-k substring back to an integer, and count it if it is non-zero and divides num. With at most 10 digits slicing is fast enough; to be strict, the substring value can be rolled forward instead.

Related

LeetCode 504: Base 7
·1 min
LeetCode Easy Math String
LeetCode 504 note: repeatedly take % 7 and // 7 of the absolute value, then add the sign and reverse; handle 0 separately
LeetCode 415: Add Strings
·2 mins
LeetCode Easy Math String Simulation
LeetCode 415 note: add digit by digit from the ends with two pointers, tracking the carry, and append any leftover carry after the loop
LeetCode 405: Convert a Number to Hexadecimal
·2 mins
LeetCode Easy Math String Bit-Manipulation
LeetCode 405 note: add 2^32 to a negative number to get its two’s complement, then take 4 bits at a time (% 16); handle 0 separately
LeetCode 412: Fizz Buzz
·1 min
LeetCode Easy Math String Simulation
LeetCode 412 note: check multiples of 15 first, then 5 and 3; multiples need %, not //
LeetCode 171: Excel Sheet Column Number
·1 min
LeetCode Easy Math String
LeetCode 171 note: scan left to right with res = res × 26 + letter value (A = 1)
LeetCode 168: Excel Sheet Column Title
·2 mins
LeetCode Easy Math String
LeetCode 168 note: Excel columns are base 26 without a zero digit; take (n - 1) % 26 as the letter, then n = (n - 1) // 26