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

<p>Return<em> the <strong>largest</strong> possible value of </em><code>num</code><em> after <strong>any</strong> number of swaps.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> num = 1234
<strong>Output:</strong> 3412
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> num = 65875
<strong>Output:</strong> 87655
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def largestInteger(self, num: int) -> int:
        # Convert the number to a list of characters (digits)
        str_num = list(str(num))
        
        # Separate odd and even digits
        list_odd = [int(digit) for digit in str_num if int(digit) % 2 != 0]
        list_even = [int(digit) for digit in str_num if int(digit) % 2 == 0]
        
        # Sort both lists in descending order
        list_odd.sort(reverse=True)
        list_even.sort(reverse=True)
        
        # Rebuild the result by replacing the original digits
        for i in range(len(str_num)):
            if int(str_num[i]) % 2 == 0:
                str_num[i] = str(list_even.pop(0))  # Replace even digit
            else:
                str_num[i] = str(list_odd.pop(0))   # Replace odd digit
        
        # Join the list back into a string and convert it to an integer
        return int(''.join(str_num))

29