LeetCode 2992: Number of Self-Divisible Permutations Solution

Master LeetCode problem 2992 (Number of Self-Divisible Permutations), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2992. Number of Self-Divisible Permutations

Problem Explanation

Explanation:

Given a string s, we want to count the number of unique permutations of the string such that each permutation is self-divisible. A string is self-divisible if every substring of length i is divisible by i.

To solve this problem, we can use backtracking to generate all permutations of the string and check if each permutation satisfies the self-divisibility property. We can also optimize the process by pruning branches early if we encounter a violation of the self-divisibility property.

Algorithm:

  1. Initialize a counter to keep track of the number of valid self-divisible permutations.
  2. Implement a backtracking function that takes the current permutation, the set of used indices, and the original string as parameters.
  3. In the backtracking function:
    • Base case: If the current permutation's length is equal to the original string's length, check if it is self-divisible. If so, increment the counter.
    • Iterate over each index in the original string:
      • If the index is not used, add it to the current permutation, mark it as used, and recursively call the function.
      • After the recursive call, backtrack by removing the index from the current permutation and marking it as unused.
  4. Return the counter as the result.

Time Complexity:

The time complexity of this algorithm is O(N!), where N is the length of the input string. This is because we are generating all permutations of the string.

Space Complexity:

The space complexity is O(N) to store the current permutation and set of used indices.

:

Solution Code

class Solution {
    public int countSelfDividingPermutations(String s) {
        int[] count = new int[1];
        backtrack("", new boolean[s.length()], s, count);
        return count[0];
    }
    
    private void backtrack(String current, boolean[] used, String s, int[] count) {
        if (current.length() == s.length()) {
            if (isSelfDivisible(current)) {
                count[0]++;
            }
            return;
        }
        
        for (int i = 0; i < s.length(); i++) {
            if (!used[i]) {
                current += s.charAt(i);
                used[i] = true;
                backtrack(current, used, s, count);
                current = current.substring(0, current.length() - 1);
                used[i] = false;
            }
        }
    }
    
    private boolean isSelfDivisible(String s) {
        for (int i = 1; i <= s.length(); i++) {
            if (Integer.parseInt(s.substring(0, i)) % i != 0) {
                return false;
            }
        }
        return true;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2992 (Number of Self-Divisible Permutations)?

This page provides optimized solutions for LeetCode problem 2992 (Number of Self-Divisible Permutations) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2992 (Number of Self-Divisible Permutations)?

The time complexity for LeetCode 2992 (Number of Self-Divisible Permutations) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2992 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2992 in Java, C++, or Python.

Back to LeetCode Solutions