1432. Max Difference You Can Get From Changing an Integer LeetCode Solution

In this guide, you will get 1432. Max Difference You Can Get From Changing an Integer LeetCode Solution with the best time and space complexity. The solution to Max Difference You Can Get From Changing an Integer problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Max Difference You Can Get From Changing an Integer solution in C++
  4. Max Difference You Can Get From Changing an Integer solution in Java
  5. Max Difference You Can Get From Changing an Integer solution in Python
  6. Additional Resources
1432. Max Difference You Can Get From Changing an Integer LeetCode Solution image

Problem Statement of 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

Complexity Analysis

  • Time Complexity: O(\log\texttt{num})
  • Space Complexity: O(\log\texttt{num})

1432. Max Difference You Can Get From Changing an Integer LeetCode Solution in C++

class Solution {
 public:
  int maxDiff(int num) {
    const string s = to_string(num);
    int firstNot9 = s.find_first_not_of('9');
    int firstNot01 = s.find_first_not_of("01");
    if (firstNot9 == string::npos)
      firstNot9 = 0;
    if (firstNot01 == string::npos)
      firstNot01 = 0;

    string a = s;
    string b = s;
    replace(a.begin(), a.end(), s[firstNot9], '9');
    replace(b.begin(), b.end(), s[firstNot01], firstNot01 == 0 ? '1' : '0');
    return stoi(a) - stoi(b);
  }
};
/* code provided by PROGIEZ */

1432. Max Difference You Can Get From Changing an Integer LeetCode Solution in Java

class Solution {
  public int maxDiff(int num) {
    final String s = String.valueOf(num);
    final int firstNot9 = firstNot(s, '9', '9');
    final int firstNot01 = firstNot(s, '0', '1');
    final String a = s.replace(s.charAt(firstNot9), '9');
    final String b = s.replace(s.charAt(firstNot01), firstNot01 == 0 ? '1' : '0');
    return Integer.parseInt(a) - Integer.parseInt(b);
  }

  private int firstNot(final String s, char a, char b) {
    for (int i = 0; i < s.length(); ++i)
      if (s.charAt(i) != a && s.charAt(i) != b)
        return i;
    return 0;
  }
}
// code provided by PROGIEZ

1432. Max Difference You Can Get From Changing an Integer LeetCode Solution in Python

class Solution:
  def maxDiff(self, num: int) -> int:
    s = str(num)

    def firstNot(s: str, t: str) -> int:
      for i, c in enumerate(s):
        if all(c != d for d in t):
          return i
      return 0

    firstNot9 = firstNot(s, '9')
    firstNot01 = firstNot(s, '01')
    a = s.replace(s[firstNot9], '9')
    b = s.replace(s[firstNot01], '1' if firstNot01 == 0 else '0')
    return int(a) - int(b)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.