2566. Maximum Difference by Remapping a Digit

Difficulty:
Related Topics:
    Similar Questions:

      Problem

      You are given an integer num. You know that Danny Mittal will sneakily remap one of the 10 possible digits (0 to 9) to another digit.

      Return the difference between the maximum and minimum values Danny can make by remapping exactly* one digit* in **num.

      Notes:

        Example 1:

      Input: num = 11891
      Output: 99009
      Explanation: 
      To achieve the maximum value, Danny can remap the digit 1 to the digit 9 to yield 99899.
      To achieve the minimum value, Danny 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:

      Solution (Java)

      class Solution {
          public int minMaxDifference(int num) {
              StringBuilder n = new StringBuilder(Integer.toString(num));
              int len = n.length();
              StringBuilder max = new StringBuilder();
              boolean flag = false;
              char f = ' ';
      
              for(int i=0;i<len;i++){
                  char ch = n.charAt(i);
      
                  if(ch == f) max.append('9');
      
                  else if(ch != '9' && !flag){
                      f=ch;
                      flag=true;
                      max.append('9');
                  }
                  else max.append(ch);
              }
      
              StringBuilder min = new StringBuilder();
              min.append('0');
              f = n.charAt(0);
      
              for(int i=1;i<len;i++){
                  char ch = n.charAt(i);
                  if(ch == f) min.append('0');
                  else min.append(ch);
              }
              return Integer.valueOf(max.toString()) - Integer.valueOf(min.toString());
          }
      }
      

      Explain:

      nope.

      Complexity: