712118. Count Good numbers

Medium

Here is a detailed Markdown blog post for the "Count Good numbers" problem:

Count Good numbers

Summary

The "Count Good numbers" problem involves counting the number of good numbers within a given range. A good number is defined as an integer that has the same digits as the number 1, but in any order. For example, the good numbers for the input 1 are 1 and the numbers that can be formed by rearranging its digits. The goal is to write an algorithm that efficiently counts the total number of good numbers within a given range.

Detailed Explanation

To solve this problem, we can start by generating all possible permutations of the digits in the given range. We then check if each permutation is a good number and count it accordingly. This approach has a time complexity of O(n!), where n is the number of digits in the given range.

Here's a step-by-step breakdown of the solution:

  1. Initialize an empty set to store the good numbers.
  2. Generate all permutations of the digits in the given range using recursion or an iterative algorithm.
  3. For each permutation, check if it is a good number by comparing its digits with those of the original number. If the permutation has the same digits as the original number, add it to the set of good numbers.
  4. Count the total number of good numbers in the set and return the result.

Here's an ASCII art diagram illustrating the algorithm:

          +---------------+
          |  Generate    |
          |  permutations |
          +---------------+
                  |
                  |  Check if each
                  |  permutation is a
                  |  good number
                  v
+-------------------------------+
|  If it's a good number, add   |
|  it to the set of good numbers |
+-------------------------------+

The time complexity of this algorithm is O(n!), where n is the number of digits in the given range. The space complexity is O(n), as we need to store the permutations and good numbers.

Optimized Solutions

Here are optimized solutions for the problem in different programming languages:

Java

View on GeeksforGeeks
import java.util.*;

public class CountGoodNumbers {
    public static int countGoodNumbers(int n) {
        Set<String> goodNumbers = new HashSet<>();
        generatePermutations(goodNumbers, String.valueOf(n));
        return goodNumbers.size();
    }

    private static void generatePermutations(Set<String> set, String str) {
        if (str.length() == 1) {
            set.add(str);
        } else {
            for (int i = 0; i < str.length(); i++) {
                String remainingStr = str.substring(0, i) + str.substring(i + 1);
                generatePermutations(set, remainingStr + str.charAt(i));
            }
        }
    }

    public static void main(String[] args) {
        int n = 1;
        System.out.println("Number of good numbers: " + countGoodNumbers(n));
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.