title: Form Smallest Number Using One Swap Operation
signature: function formSmallestNumberUsingOneSwapOperation(num: string): string {
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_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
question_info.test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value: any): string{
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual: string, expected: string): boolean {
      return convertToString(actual) === convertToString(expected);
  }
  
  function driver(num: string, expected:string): string {
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(num),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver("ncYltuhSxEfG", "EcYltuhSxnfG");
    console.log(`TEST-0...${result}`);
  
    result = driver("26615541616459", "16625541616459");
    console.log(`TEST-1...${result}`);
  
    result = driver("0101", "0011");
    console.log(`TEST-2...${result}`);
  
    result = driver("hK", "Kh");
    console.log(`TEST-3...${result}`);
  
    result = driver("422162103899", "122462103899");
    console.log(`TEST-4...${result}`);
  
    result = driver("0010", "0001");
    console.log(`TEST-5...${result}`);
  
    result = driver("zfcSh", "Sfczh");
    console.log(`TEST-6...${result}`);
  
    result = driver("92", "29");
    console.log(`TEST-7...${result}`);
  
    result = driver("0", "0");
    console.log(`TEST-8...${result}`);
  
    result = driver("v", "v");
    console.log(`TEST-9...${result}`);
  
  };
  
  main();
question_info.extension: ts
question_info.entry_fn_name: formSmallestNumberUsingOneSwapOperation
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: tsc --target es2020 --lib es5,dom,es2015,es2020 __FILENAME__
question_info.commands: node __FILENAME__.js