MicrosoftOA #47
Minimize Difference Between Two Numbers
Estimated MediumJudge-ready
The two strings have the same length and no leading zeros. One operation swaps the digits of the two strings at the same position. First, the absolute difference of the final values must be as small as possible; then, among those options, minimize the number of swaps.
This site uses a standard input/output protocol.
Input
Two lines S and T, of length 1..100000, with characters 0..9 and no leading zeros.
Output
Output a single integer, the answer.
Example 1
Input:
29162
10524
Output:
2
Example 2
Input:
123
123
Output:
0
Example 3
Input:
19
21
Output:
0
Example notes
Example 1: swapping positions 2 and 4 gives 20122 and 19564, with a difference of 558, using 2 swaps. Example 2: the strings are already identical, so no swap is needed. Example 3: the ones digit already offsets the tens difference, and the original difference 2 is minimal, so 0 swaps.
Editorial
Loading solution…