Talentd
FleetCode

3272. Find the Count of Good Integers

Hard69.5% acceptance2 companies

You are given two positive integers n and k.

An integer x is called k-palindromic if:

  • x is a palindrome.
  • x is divisible by k.

An integer is called good if its digits can be rearranged to form a k-palindromic integer. For example, for k = 2, 2020 can be rearranged to form the k-palindromic integer 2002, whereas 1010 cannot be rearranged to form a k-palindromic integer.

Return the count of good integers containing n digits.

Note that any integer must not have leading zeros, neither before nor after rearrangement. For example, 1010 cannot be rearranged to form 101.

Example 1:

Input: n = 3, k = 5

Output: 27

Explanation:

Some of the good integers are:

  • 551 because it can be rearranged to form 515.
  • 525 because it is already k-palindromic.

Example 2:

Input: n = 1, k = 4

Output: 2

Explanation:

The two good integers are 4 and 8.

Example 3:

Input: n = 5, k = 6

Output: 2468

Constraints:

  • 1 <= n <= 10
  • 1 <= k <= 9

Asked at 2 companies

Hints open one at a time

Hint 1
How to generate all K-palindromic strings of length n? Do we need to go through all n digits?
Hint 2
Use permutations to calculate the number of possible rearrangements.
49:26
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countGoodIntegers(self, n: int, k: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
3
5
Input
1
4
Input
5
6
Your code is saved on this device.Solution