title: Form Smallest Number Using One Swap Operation
signature: def form_smallest_number_using_one_swap_operation(num):
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;
  }
question_info.test_code: import array
  import bisect
  import collections
  import datetime
  import functools
  import heapq
  import itertools
  import math
  import queue
  import re
  import string
  import sys
  from typing import Any, Dict, List, Optional, Set
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  def validate_solution(actual, expected):
      return actual == expected
  
  def driver(num, expected):
      try:
          if validate_solution(PLACEHOLDER_FN_NAME(
              num),
              expected
          ):
              return "PASSED"
          return "FAILED"
      except Exception as exception_obj:
          return type(exception_obj).__name__
      
  if __name__ == "__main__":
      result = driver("ncYltuhSxEfG", "EcYltuhSxnfG")
      print(f"TEST-0...{result}")
  
      result = driver("26615541616459", "16625541616459")
      print(f"TEST-1...{result}")
  
      result = driver("0101", "0011")
      print(f"TEST-2...{result}")
  
      result = driver("hK", "Kh")
      print(f"TEST-3...{result}")
  
      result = driver("422162103899", "122462103899")
      print(f"TEST-4...{result}")
  
      result = driver("0010", "0001")
      print(f"TEST-5...{result}")
  
      result = driver("zfcSh", "Sfczh")
      print(f"TEST-6...{result}")
  
      result = driver("92", "29")
      print(f"TEST-7...{result}")
  
      result = driver("0", "0")
      print(f"TEST-8...{result}")
  
      result = driver("v", "v")
      print(f"TEST-9...{result}")
  
      sys.exit(0)
question_info.test_case_ids: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
question_info.entry_fn_name: form_smallest_number_using_one_swap_operation
question_info.test_list: [{"idx": 0, "outputs": "EcYltuhSxnfG", "inputs": {"num": "ncYltuhSxEfG"}}, {"idx": 1, "outputs": "16625541616459", "inputs": {"num": "26615541616459"}}, {"idx": 2, "outputs": "0011", "inputs": {"num": "0101"}}, {"idx": 3, "outputs": "Kh", "inputs": {"num": "hK"}}, {"idx": 4, "outputs": "122462103899", "inputs": {"num": "422162103899"}}, {"idx": 5, "outputs": "0001", "inputs": {"num": "0010"}}, {"idx": 6, "outputs": "Sfczh", "inputs": {"num": "zfcSh"}}, {"idx": 7, "outputs": "29", "inputs": {"num": "92"}}, {"idx": 8, "outputs": "0", "inputs": {"num": "0"}}, {"idx": 9, "outputs": "v", "inputs": {"num": "v"}}]
question_info.entry_cls_name: Solution