title: Form Smallest Number Using One Swap Operation
signature: pub fn form_smallest_number_using_one_swap_operation(num: String) -> String {
test_code: use std::collections::HashMap;
  use std::collections::HashSet;
  use std::cmp::*;
  use std::collections::*;
  use std::fmt::*; 
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  pub fn validate_solution(actual: String, expected: String)->bool{
  return actual==expected
  }
  pub fn driver(num: String, expected: String)->String{
      if validate_solution(PLACEHOLDER_FN_NAME(num), expected) {
        return "PASSED".to_string();
      };
      return "FAILED".to_string();
  
  }
  
  fn main() {
      println!("TEST-0...{}", driver(
        "ncYltuhSxEfG".to_string(), 
        "EcYltuhSxnfG".to_string()
      ));
      
      println!("TEST-1...{}", driver(
        "26615541616459".to_string(), 
        "16625541616459".to_string()
      ));
      
      println!("TEST-2...{}", driver(
        "0101".to_string(), 
        "0011".to_string()
      ));
      
      println!("TEST-3...{}", driver(
        "hK".to_string(), 
        "Kh".to_string()
      ));
      
      println!("TEST-4...{}", driver(
        "422162103899".to_string(), 
        "122462103899".to_string()
      ));
      
      println!("TEST-5...{}", driver(
        "0010".to_string(), 
        "0001".to_string()
      ));
      
      println!("TEST-6...{}", driver(
        "zfcSh".to_string(), 
        "Sfczh".to_string()
      ));
      
      println!("TEST-7...{}", driver(
        "92".to_string(), 
        "29".to_string()
      ));
      
      println!("TEST-8...{}", driver(
        "0".to_string(), 
        "0".to_string()
      ));
      
      println!("TEST-9...{}", driver(
        "v".to_string(), 
        "v".to_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;
  }