2566. Maximum Difference by Remapping a Digit LeetCode Solution

In this guide, you will get 2566. Maximum Difference by Remapping a Digit LeetCode Solution with the best time and space complexity. The solution to Maximum Difference by Remapping a Digit 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. Maximum Difference by Remapping a Digit solution in C++
  4. Maximum Difference by Remapping a Digit solution in Java
  5. Maximum Difference by Remapping a Digit solution in Python
  6. Additional Resources
2566. Maximum Difference by Remapping a Digit LeetCode Solution image

Problem Statement of Maximum Difference by Remapping a Digit

You are given an integer num. You know that Bob will sneakily remap one of the 10 possible digits (0 to 9) to another digit.
Return the difference between the maximum and minimum values Bob can make by remapping exactly one digit in num.
Notes:

When Bob remaps a digit d1 to another digit d2, Bob replaces all occurrences of d1 in num with d2.
Bob can remap a digit to itself, in which case num does not change.
Bob can remap different digits for obtaining minimum and maximum values respectively.
The resulting number after remapping can contain leading zeroes.

Example 1:

Input: num = 11891
Output: 99009
Explanation:
To achieve the maximum value, Bob can remap the digit 1 to the digit 9 to yield 99899.
To achieve the minimum value, Bob can remap the digit 1 to the digit 0, yielding 890.
The difference between these two numbers is 99009.

Example 2:

Input: num = 90
Output: 99
Explanation:
The maximum value that can be returned by the function is 99 (if 0 is replaced by 9) and the minimum value that can be returned by the function is 0 (if 9 is replaced by 0).
Thus, we return 99.

Constraints:

1 <= num <= 108

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2566. Maximum Difference by Remapping a Digit LeetCode Solution in C++

class Solution {
 public:
  int minMaxDifference(int num) {
    const string s = to_string(num);
    const char to9 = s[firstNotNineIndex(s)];
    const char to0 = s[0];
    return getMax(s, to9) - getMin(s, to0);
  }

 private:
  int firstNotNineIndex(const string& s) {
    for (int i = 0; i < s.length(); ++i)
      if (s[i] != '9')
        return i;
    return 0;
  }

  int getMax(string s, char to9) {
    for (char& c : s)
      if (c == to9)
        c = '9';
    return stoi(s);
  }

  int getMin(string s, char to0) {
    for (char& c : s)
      if (c == to0)
        c = '0';
    return stoi(s);
  }
};
/* code provided by PROGIEZ */

2566. Maximum Difference by Remapping a Digit LeetCode Solution in Java

class Solution {
  public int minMaxDifference(int num) {
    final String s = String.valueOf(num);
    final char to9 = s.charAt(firstNotNineIndex(s));
    final char to0 = s.charAt(0);
    return getMax(new StringBuilder(s), to9) - getMin(new StringBuilder(s), to0);
  }

  private int firstNotNineIndex(final String s) {
    for (int i = 0; i < s.length(); ++i)
      if (s.charAt(i) != '9')
        return i;
    return 0;
  }

  private int getMax(StringBuilder sb, char to9) {
    for (int i = 0; i < sb.length(); ++i)
      if (sb.charAt(i) == to9)
        sb.setCharAt(i, '9');
    return Integer.parseInt(sb.toString());
  }

  private int getMin(StringBuilder sb, char to0) {
    for (int i = 0; i < sb.length(); ++i)
      if (sb.charAt(i) == to0)
        sb.setCharAt(i, '0');
    return Integer.parseInt(sb.toString());
  }
}
// code provided by PROGIEZ

2566. Maximum Difference by Remapping a Digit LeetCode Solution in Python

class Solution:
  def minMaxDifference(self, num: int) -> int:
    s = str(num)
    to9 = s[self._firstNotNineIndex(s)]
    to0 = s[0]
    return int(s.replace(to9, '9')) - int(s.replace(to0, '0'))

  def _firstNotNineIndex(self, s: str) -> int:
    for i, c in enumerate(s):
      if c != '9':
        return i
    return 0
# code by PROGIEZ

Additional Resources

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