{x}
blog image

Max Difference You Can Get From Changing an Integer

You are given an integer num. You will apply the following steps exactly two times:

  • Pick a digit x (0 <= x <= 9).
  • Pick another digit y (0 <= y <= 9). The digit y can be equal to x.
  • Replace all the occurrences of x in the decimal representation of num by y.
  • The new integer cannot have any leading zeros, also the new integer cannot be 0.

Let a and b be the results of applying the operations to num the first and second times, respectively.

Return the max difference between a and b.

 

Example 1:

Input: num = 555
Output: 888
Explanation: The first time pick x = 5 and y = 9 and store the new integer in a.
The second time pick x = 5 and y = 1 and store the new integer in b.
We have now a = 999 and b = 111 and max difference = 888

Example 2:

Input: num = 9
Output: 8
Explanation: The first time pick x = 9 and y = 9 and store the new integer in a.
The second time pick x = 9 and y = 1 and store the new integer in b.
We have now a = 9 and b = 1 and max difference = 8

 

Constraints:

  • 1 <= num <= 108

Solution Explanation

The problem asks to find the maximum difference between two numbers, a and b, derived from a given integer num by applying a specific transformation twice. The transformation involves selecting digits x and y and replacing all occurrences of x in num with y. The goal is to maximize the difference a - b.

The solution employs a greedy approach. To maximize a, we want to replace a digit (other than 9) with 9, thus making the number as large as possible. To minimize b, if the leading digit is not 1, we replace it with 1. Otherwise, we find a non-zero digit (other than 1) and replace it with 0.

Algorithm

  1. Convert to String: Convert the input integer num to its string representation for easier manipulation of digits.

  2. Maximize a: Iterate through the string representation of num. If a digit other than '9' is found, replace all occurrences of that digit with '9' and break the loop. This ensures that we get the largest possible number a.

  3. Minimize b:

    • If the leading digit of num is not '1', replace all occurrences of the leading digit with '1'.
    • Otherwise (leading digit is '1'), iterate from the second digit. If a digit other than '0' and '1' is found, replace all occurrences of that digit with '0'. This ensures we get the smallest possible number b, avoiding leading zeros.
  4. Calculate Difference: Convert the modified strings a and b back to integers and return their difference a - b.

Time and Space Complexity Analysis

  • Time Complexity: O(N), where N is the number of digits in the input integer num. The string manipulations (replacements) take linear time.

  • Space Complexity: O(N), dominated by the space used to store the string representations of a and b. In some languages, the string might be modified in-place, leading to O(1) space complexity.

Code Explanation (Python)

The Python code efficiently implements the algorithm:

class Solution:
    def maxDiff(self, num: int) -> int:
        a, b = str(num), str(num)  # Convert to strings
        for c in a:                # Maximize 'a'
            if c != "9":
                a = a.replace(c, "9")
                break
        if b[0] != "1":            # Minimize 'b' (leading digit not 1)
            b = b.replace(b[0], "1")
        else:                       # Minimize 'b' (leading digit is 1)
            for c in b[1:]:
                if c not in "01":
                    b = b.replace(c, "0")
                    break
        return int(a) - int(b)     # Return the difference

The other languages (Java, C++, Go) follow a similar structure, adapting string manipulation techniques specific to each language. The core logic remains consistent across all implementations.