static void nPermute ( String str , int n ) {
  precomputeFactorirals ( ) ;
  int len = str . length ( ) ;
  int freq [ ] = new int [ MAX_CHAR ] ;
  for ( int i = 0 ;
  i < len ;
  i ++ ) freq [ str . charAt ( i ) - 'a' ] ++ ;
  String out = "" ;
  int sum = 10 ;
  int k = 0 ;
  while ( sum >= n ) {
    for ( int i = 0 ;
    i < MAX_CHAR ;
    i ++ ) {
      if ( freq [ i ] == 0 ) continue ;
      freq [ i ] -- ;
      sum = 0 ;
      int xsum = ( int ) fact [ len - 1 - k ] ;
      for ( int j = 0 ;
      j < MAX_CHAR ;
      j ++ ) xsum /= fact [ freq [ j ] ] ;
      sum += xsum ;
      if ( sum >= n ) {
        out += ( char ) ( i + 'a' ) ;
        k ++ ;
        n -= ( sum - xsum ) ;
        break ;
      }
      if ( sum < n ) freq [ i ] ++ ;
    }
  }
  for ( int i = MAX_CHAR - 1 ;
  k < len && i >= 0 ;
  i -- ) if ( freq [ i ] != 0 ) {
    out += ( char ) ( i + 'a' ) ;
    freq [ i ++ ] -- ;
  }
  System . out . println ( out ) ;
}

static void nPermute ( String str , int n ) { precomputeFactorirals ( ) ; int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < len ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; String out = " " ; int sum = 10 ; int k = 0 ; while ( sum >= n ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; int xsum = ( int ) fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= fact [ freq [ j ] ] ; sum += xsum ; if ( sum >= n ) { out += ( char ) ( i + ' a ' ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { out += ( char ) ( i + ' a ' ) ; freq [ i ++ ] -- ; } System . out . println ( out ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (expression_statement (method_invocation name: (identifier) arguments: (argument_list))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (update_expression (array_access array: (identifier) index: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (string_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) body: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (continue_statement)) (expression_statement (update_expression (array_access array: (identifier) index: (identifier)))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (array_access array: (identifier) index: (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (identifier)))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (array_access array: (identifier) index: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (cast_expression type: (integral_type) value: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal)))))) (expression_statement (update_expression (identifier))) (expression_statement (assignment_expression left: (identifier) right: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))))) (break_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (expression_statement (update_expression (array_access array: (identifier) index: (identifier))))))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (cast_expression type: (integral_type) value: (parenthesized_expression (binary_expression left: (identifier) right: (character_literal)))))) (expression_statement (update_expression (array_access array: (identifier) index: (update_expression (identifier)))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier))))))