Rearrange digits of a number to remove any subsequence of another given number

Given two numeric strings N and K (K ≤ N), where digits of K are in non-decreasing order, the task is to rearrange digits of N such that K does not appear as a subsequence in N. If it is not possible to obtain such a permutation, print “-1”. Otherwise print any such valid permutation of N.

Examples:

Input: N = 93039373637, K = 339
Output: 97093736333

Input: N=965, K=55
Output: 965

Naive Approach: The simplest approach is to generate every permutation of N and check for each permutation, if K appears as a subsequence in it or not. If found to be false for any permutation, print that permutation.
Time Complexity: O(N*N!)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the observation that the digits of K are in non-decreasing order. Therefore, rearrange the digits of N in decreasing order by sorting. Follow the steps below to solve the problem:



  • Store the frequency of digits of N and K in HashMaps M1 and M2, respectively.
  • Iterate over the range [0, 9] using a variable, say i, to check if K has any digit with more occurrences than in N. If M2[i] > M1[i], then print N and return.
  • Again, iterate over the range [0, 9] using a variable i to check if K contains only 1 distinct digit. If M2[i] is the same as length(K), then print “-1” and return. 
  • For all other cases, sort N in decreasing order and then print N.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find a permutation of
// number N such that the number K does
// not appear as a subsequence in N
void findArrangement(string n, string k)
{
  
    // Stores frequency of digits of N
    unordered_map<char, int> um1;
    for (int i = 0; i < n.size(); i++) {
        um1[n[i]]++;
    }
  
    // Stores frequency of digits of K
    unordered_map<char, int> um2;
    for (int i = 0; i < k.size(); i++) {
        um2[k[i]]++;
    }
  
    // Check if any digit in K has
    // more occurrences than in N
    for (int i = 0; i <= 9; i++) {
        char ch = '0' + i;
  
        // If true, print N and return
        if (um2[ch] > um1[ch]) {
            cout << n;
            return;
        }
    }
  
    // Check if K contains only a
    // single distinct digit
    for (int i = 0; i <= 9; i++) {
        char ch = '0' + i;
  
        // If true, print -1 and return
        if (um2[ch] == k.size()) {
            cout << -1;
            return;
        }
    }
  
    // For all other cases, sort N
    // in decreasing order
    sort(n.begin(), n.end(),
         greater<char>());
  
    // Print the value of N
    cout << n;
}
  
// Driver Code
int main()
{
    string N = "93039373637", K = "339";
  
    // Function Call
    findArrangement(N, K);
  
    return 0;
}

chevron_right


Output:

99776333330

Time Complexity: O(L*log (L)), where L is the size of the string N
Auxiliary Space: O(L)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.