Given two numbers N and M. Construct maximal number by permuting (changing order) the digits of N, not exceeding M. 
It is allowed to leave N as it is. If there's no Permutation of digits of N which doesn't exceed M, then print -1.
Note : Answer shouldn't have any leading zeroes.
Example 1: 
Input:
N = 123
M = 222
Output:
213
Explanation: There are total 3! permutations 
possible for N = 123, but the only permutation 
that satisfies the given condition is 213.
Example 2: 
Input:
N =  3921
M = 10000
Output:
9321
Explanation: Among all the Possible Permutations,
9321 is Maximum.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxPerm() which takes two Integers N and M as input parameters and returns the answer.
Expected Time Complexity: O(|N|^{2})
Expected Auxiliary Space: O(|N|)
Constraints:
1 ≤ A, B ≤ 10^{7}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Combinatorics

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def maxPerm(self, N , M):
        # code here 

Solutions:
from itertools import permutations

class Solution:

	def maxPerm(self, N, M):
		ans = [list(i) for i in permutations(str(N))]
		ans = [int(''.join(i)) for i in ans if int(''.join(i)) <= M]
		return max(ans) if len(ans) > 0 else -1