You are given a positive integer num. You may swap any two digits of num that have the same parity (i.e. both odd digits or both even digits).

Return the largest possible value of num after any number of swaps.

&nbsp;
Example 1:

Input: num = 1234
Output: 3412
Explanation: Swap the digit 3 with the digit 1, this results in the number 3214.
Swap the digit 2 with the digit 4, this results in the number 3412.
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.


Example 2:

Input: num = 65875
Output: 87655
Explanation: Swap the digit 8 with the digit 6, this results in the number 85675.
Swap the first digit 5 with the digit 7, this results in the number 87655.
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.


&nbsp;
Constraints:


	1 &lt;= num &lt;= 109


class Solution:
    def largestInteger(self, num: int):
        n = len(str(num))
        arr = [int(i) for i in str(num)]
        odd, even = [], []
        for i in arr:
            if i % 2 == 0:
                even.append(i)
            else:
                odd.append(i)
        odd.sort()
        even.sort()
        res = 0
        for i in range(n):
            if arr[i] % 2 == 0:
                res = res*10 + even.pop()
            else:
                res = res*10 + odd.pop()
        return res
class Solution {
    public int largestInteger(int num) {
        PriorityQueue<Integer> opq = new PriorityQueue<>();
        PriorityQueue<Integer> epq = new PriorityQueue<>();
        int bnum = num;
        while(num>0){
            int cur = num%10;
            if(cur%2==1){
                opq.add(cur);
            }else{
                epq.add(cur);
            }
            num /= 10;
        }
        StringBuilder sb = new StringBuilder();
        num = bnum;
        while(num>0){
            int cur = num%10;
            if(cur%2==1)
                sb.insert(0, opq.poll());
            else
                sb.insert(0, epq.poll());
            num /= 10;
        }
        return Integer.parseInt(sb.toString());
    }
}
class Solution {
public:
    int largestInteger(int num) {
        priority_queue<int> p; // priority queue to store odd digits in descending order
        priority_queue<int> q; // priority queue to store even digits in descending order
        string nums=to_string(num); // converting num to a string for easy access of the digits
        int n=nums.size(); // n stores the number of digits in num

        for(int i=0;i<n;i++){
            int digit=nums[i]-'0';
            if((digit)%2) // if digit is odd, push it into priority queue p
                p.push(digit);
            else
                q.push(digit); // if digit is even, push it into priority queue q
        }

        int answer=0;
        for(int i=0; i<n; i++){
            answer=answer*10;
            if((nums[i]-'0')%2) // if the digit is odd, add the largest odd digit of p into the answer
                {answer+=p.top();p.pop();}
            else
                {answer+=q.top();q.pop();} // if the digit is even, add the largest even digit of q into the answer
        }
        return answer;
    }
};
/**
 * @param {number} num
 * @return {number}
 */
var largestInteger = function(num) {
    var nums = num.toString().split("");//splitting the numbers into an array
    var odd=[];//helps keep a track of odd numbers
    var even =[];//helps keep a track of even numbers
    for(var i=0;i<nums.length;i++){
        if(nums[i]%2===0) //pushing even numbers to even array
            even.push(nums[i]);
        else//pushing odd numbers to odd array
            odd.push(nums[i]);
    }
    odd.sort((a,b)=>a-b);//sorting the arrays in ascending order
    even.sort((a,b)=>a-b);
    var ans =[];
    for(var i=0;i<nums.length;i++){
        if(nums[i]%2===0){//replacing even with even
            ans.push(even[even.length-1]);//pushing the greatest even number in the ans
            even.pop();//popping the used number
        }
        else{//replacing odd with odd
            ans.push(odd[odd.length-1]);//pushing the greatest odd number in the ans
            odd.pop();//popping the used number
        }
    }
    var ans2 = parseInt(ans.join(""));//converting the ans array into an int number
    return ans2;

};
Largest Number After Digit Swaps by Parity