Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Smallest Divisible Digit Product II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3348" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a string num which represents a positive integer, and an integer t. A number is called zero-free if none of its digits are 0. Return a string representing the smallest zero-free number greater than or equal to num such that the product of its digits is divisible by t. If no such number exists, return "-1". Example 1: Input: num = "1234", t = 256 Output: "1488" Explanation: The smallest zero-free number that is greater than 1234 and has the product of its digits divisible by 256 is 1488, with the product of its digits equal to 256. Example 2: Input: num = "12355", t = 50 Output: "12355" Explanation: 12355 is already zero-free and has the product of its digits divisible by 50, with the product of its digits equal to 150. Example 3: Input: num = "11111", t = 26 Output: "-1" Explanation: No number greater than 11111 has the product of its digits divisible by 26. Constraints: 2 <= num.length <= 2 * 105 num consists only of digits in the range ['0', '9']. num does not contain leading zeros. 1 <= t <= 1014

Explanation

  • Check Divisibility and Increment: First, check if the given number num itself satisfies the zero-free and divisibility conditions. If so, return num. Otherwise, increment num (simulating adding 1) and ensure it remains zero-free.
    • Backtracking Search: If the incremented number's digit product is not divisible by t, employ a backtracking-like approach to explore increasing digits from right to left. If at any point we hit a '0' when incrementing, we directly change the digit to '1'.
    • Optimized Digit Replacement: Optimize the search by intelligently replacing digits with their smallest possible zero-free values while prioritizing divisibility.
  • Runtime Complexity: O(N 9 log(T)), where N is the length of the input string num, and T is the target number. Storage Complexity: O(N)

Code

    def solve():
    num = input()
    t = int(input())

    def is_zero_free(s):
        return '0' not in s

    def product_of_digits(s):
        prod = 1
        for digit in s:
            prod *= int(digit)
        return prod

    def find_smallest_zero_free(num_str, target):
        n = len(num_str)
        num_list = list(num_str)

        while True:
            if is_zero_free("".join(num_list)):
                prod = product_of_digits("".join(num_list))
                if prod % target == 0:
                    return "".join(num_list)


            i = n - 1
            while i >= 0 and num_list[i] == '9':
                num_list[i] = '1'
                i -= 1

            if i == -1:
                num_list.insert(0, '1')
                n += 1
            else:
                num_list[i] = str(int(num_list[i]) + 1)
                if num_list[i] == '0':
                   num_list[i] = '1'

            if not is_zero_free("".join(num_list)):

              j = n - 1
              while j >= 0:
                  if num_list[j] == '0':
                    num_list[j] = '1'
                  j -= 1

    if is_zero_free(num) and product_of_digits(num) % t == 0:
        print(num)
        return

    result = find_smallest_zero_free(num, t)

    if result:
        print(result)
    else:
        print("-1")

solve()

More from this blog

C

Chatmagic blog

2894 posts