public static String smallestPermutation ( String s , int p , int q ) {
  if ( check ( s , p , q ) == true ) {
    return "Impossible" ;
  }
  char tempArray [ ] = s . toCharArray ( ) ;
  Arrays . sort ( tempArray ) ;
  String str = new String ( tempArray ) ;
  int a = nx * ny , b = 0 , i = 0 , j = 0 ;
  if ( a == p && b == q ) {
    return str ;
  }
  while ( 1 > 0 ) {
    for ( i = 0 ;
    i < str . length ( ) - 1 ;
    ++ i ) {
      if ( str . charAt ( i ) == 'x' && str . charAt ( i + 1 ) == 'y' ) break ;
    }
    for ( j = i ;
    j < str . length ( ) - 1 ;
    j ++ ) {
      if ( str . charAt ( j ) == 'x' && str . charAt ( j + 1 ) == 'y' ) {
        StringBuilder sb = new StringBuilder ( str ) ;
        sb . setCharAt ( j , str . charAt ( j + 1 ) ) ;
        sb . setCharAt ( j + 1 , str . charAt ( j ) ) ;
        str = sb . toString ( ) ;
        a -- ;
        b ++ ;
        if ( a == p && b == q ) {
          return str ;
        }
      }
    }
  }
}

public static String smallestPermutation ( String s , int p , int q ) { if ( check ( s , p , q ) == true ) { return " Impossible " ; } char tempArray [ ] = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; String str = new String ( tempArray ) ; int a = nx * ny , b = 0 , i = 0 , j = 0 ; if ( a == p && b == q ) { return str ; } while ( 1 > 0 ) { for ( i = 0 ; i < str . length ( ) - 1 ; ++ i ) { if ( str . charAt ( i ) == ' x ' && str . charAt ( i + 1 ) == ' y ' ) break ; } for ( j = i ; j < str . length ( ) - 1 ; j ++ ) { if ( str . charAt ( j ) == ' x ' && str . charAt ( j + 1 ) == ' y ' ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setCharAt ( j , str . charAt ( j + 1 ) ) ; sb . setCharAt ( j + 1 , str . charAt ( j ) ) ; str = sb . toString ( ) ; a -- ; b ++ ; if ( a == p && b == q ) { return str ; } } } } }
(method_declaration (modifiers) type: (type_identifier) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation name: (identifier) arguments: (argument_list (identifier) (identifier) (identifier))) right: (true))) consequence: (block (return_statement (string_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (identifier))) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal)) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (block (return_statement (identifier)))) (while_statement condition: (parenthesized_expression (binary_expression left: (decimal_integer_literal) right: (decimal_integer_literal))) body: (block (for_statement init: (assignment_expression left: (identifier) right: (decimal_integer_literal)) condition: (binary_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (character_literal)))) consequence: (break_statement)))) (for_statement init: (assignment_expression left: (identifier) right: (identifier)) condition: (binary_expression left: (identifier) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (character_literal)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))) right: (character_literal)))) consequence: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) consequence: (block (return_statement (identifier))))))))))))