title: Form Smallest Number Using One Swap Operation
signature: function form_smallest_number_using_one_swap_operation(num)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(num, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(num) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver("ncYltuhSxEfG", "EcYltuhSxnfG")
      print(string.format("TEST-0...%s",result))
  
      result = driver("26615541616459", "16625541616459")
      print(string.format("TEST-1...%s",result))
  
      result = driver("0101", "0011")
      print(string.format("TEST-2...%s",result))
  
      result = driver("hK", "Kh")
      print(string.format("TEST-3...%s",result))
  
      result = driver("422162103899", "122462103899")
      print(string.format("TEST-4...%s",result))
  
      result = driver("0010", "0001")
      print(string.format("TEST-5...%s",result))
  
      result = driver("zfcSh", "Sfczh")
      print(string.format("TEST-6...%s",result))
  
      result = driver("92", "29")
      print(string.format("TEST-7...%s",result))
  
      result = driver("0", "0")
      print(string.format("TEST-8...%s",result))
  
      result = driver("v", "v")
      print(string.format("TEST-9...%s",result))
  
  end
  
  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;
  }