title: Form Smallest Number Using One Swap Operation
signature: form_smallest_number_using_one_swap_operation <- function(num) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(num, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(num), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          "ncYltuhSxEfG", 
          "EcYltuhSxnfG"
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          "26615541616459", 
          "16625541616459"
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          "0101", 
          "0011"
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          "hK", 
          "Kh"
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          "422162103899", 
          "122462103899"
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          "0010", 
          "0001"
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          "zfcSh", 
          "Sfczh"
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          "92", 
          "29"
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          "0", 
          "0"
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          "v", 
          "v"
      )))
  
  }
  
  main()
source_py: def form_smallest_number_using_one_swap_operation(num):
      num = list(num)
      n = len(num)
      rightMin = ([0] * n)
      right = 0
      rightMin[(n - 1)] = (- 1)
      right = (n - 1)
      for i in range((n - 2), 0, (- 1)):
          if (num[i] > num[right]):
              rightMin[i] = right
          else:
              rightMin[i] = (- 1)
              right = i
      small = (- 1)
      for i in range(1, n):
          if (num[i] != '0'):
              if (small == (- 1)):
                  if (num[i] < num[0]):
                      small = i
              elif (num[i] < num[small]):
                  small = i
      if (small != (- 1)):
          (num[0], num[small]) = (num[small], num[0])
      else:
          for i in range(1, n):
              if (rightMin[i] != (- 1)):
                  (num[i], num[rightMin[i]]) = (num[rightMin[i]], num[i])
                  break
      return ''.join(num)
source_cpp: string form_smallest_number_using_one_swap_operation(string num) {
    int n = num . size ( );
    int rightMin [ n ], right;
    rightMin [ n - 1 ] = - 1;
    right = n - 1;
    for ( int i = n - 2;
    i >= 1;
    i -- ) {
      if ( num [ i ] >= num [ right ] ) rightMin [ i ] = right;
      else {
        if ( num [ i ] == num [ i + 1 ] ) {
          rightMin [ i ] = right;
        }
        else {
          rightMin [ i ] = - 1;
          right = i;
        }
      }
    }
    int small = - 1;
    for ( int i = 1;
    i < n;
    i ++ ) if ( num [ i ] != '0' ) {
      if ( small == - 1 ) {
        if ( num [ i ] < num [ 0 ] ) small = i;
      }
      else if ( num [ i ] <= num [ small ] ) small = i;
    }
    if ( small != - 1 ) swap ( num [ 0 ], num [ small ] );
    else {
      for ( int i = 1;
      i < n;
      i ++ ) {
        if ( rightMin [ i ] != - 1 && num [ i ] != num [ rightMin [ i ] ] ) {
          swap ( num [ i ], num [ rightMin [ i ] ] );
          break;
        }
      }
    }
    return num;
  }