Leetcode Problem 1432. Max Difference You Can Get From Changing an Integer

1432. Max Difference You Can Get From Changing an Integer

Leetcode Solutions

Maximize and Minimize Replacements

  1. Convert the integer num to a string num_str.
  2. To find the maximum number a, iterate through num_str and replace the first occurrence of a non-'9' digit with '9'.
  3. To find the minimum number b, check if the first digit is not '1'. If it's not, replace all occurrences of the first digit with '1'. If the first digit is '1', find the next digit that is not '0' or '1' and replace all occurrences of that digit with '0'.
  4. Convert the modified strings back to integers to get a and b.
  5. Return the difference a - b.
UML Thumbnail

Brute Force Digit Replacement

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...