<awangdev>301. Remove Invalid Parentheses_2.java
private void dfs(List<String> rst, String s, int x, int y, char open, char close) {

        for (int count = 0, i = x; i < s.length(); i++) {
            if (s.charAt(i) == open) count++;
            if (s.charAt(i) == close) count--;
            if (count < 0) {
                for (int j = y; j <= i; j++) {
                    if (s.charAt(j) == close && (j == y || s.charAt(j - 1) != close)) {
                        dfs(rst, s.substring(0, j) + s.substring(j+1), i, j, open, close);
                    }
                }
                return;
            }
        }
        String reverse = new StringBuffer(s).reverse().toString();
        if (open == '(') {
            dfs(rst, reverse, 0, 0, close, open);
        } else { 
            rst.add(reverse);    
        }
    }
private void sample_funcname ( List < String > rst , String s , int x , int y , char open , char close ) {
  for ( int count = 0 , i = x ;
  i < s . length ( ) ;
  i ++ ) {
    if ( s . charAt ( i ) == open ) count ++ ;
    if ( s . charAt ( i ) == close ) count -- ;
    if ( count < 0 ) {
      for ( int j = y ;
      j <= i ;
      j ++ ) {
        if ( s . charAt ( j ) == close && ( j == y || s . charAt ( j - 1 ) != close ) ) {
          dfs ( rst , s . substring ( 0 , j ) + s . substring ( j + 1 ) , i , j , open , close ) ;
        }
      }
      return ;
    }
  }
  String reverse = new StringBuffer ( s ) . reverse ( ) . toString ( ) ;
  if ( open == '(' ) {
    dfs ( rst , reverse , 0 , 0 , close , open ) ;
  }
  else {
    rst . add ( reverse ) ;
  }
}
