Compile and check my output and tell me why output is like that.(what is unsafe in this)

import java .util.*;
class N{

public static void main(String[] args) {
	
	//Scanner sc = new Scanner(System.in);

	//String str = sc.nextLine();
	int n = 2; 

	String str = "(";

	printPermutation(str,n,1,0);// 1 is count of ( and 0 is for left paren count at initial state.



}

public static void printPermutation(String ans,int n,int lpcount,int rpcount){

	if(ans.length() == n*2 ){
		
		if(isValid(ans,lpcount,rpcount)){
			System.out.println(ans);
		}	


		return ;
	}


	printPermutation(ans+"(", n ,lpcount+1,rpcount);


	printPermutation(ans+")", n ,lpcount,rpcount+1);


}


public static boolean isValid(String ques,int lpcount , int rpcount){

		if(lpcount == rpcount){
			
			Stack<Character> st = new Stack();

			for(int i=0;i<ques.length();i++){

				char ch = ques.charAt(i);

				if(ch == '('){
					st.push(ch);
				}else{
					if(st.isEmpty() == false){
					st.pop();
				}

				}


			}
		
			if(st.size() ==0){
				return  true;
			}
		}

		return false;
}

}

@amankumar7017,
https://ide.codingblocks.com/s/279514 corrected code.

Safety error:
Use Stack<Character> st = new Stack<Character>(); instead of Stack<Character> st = new Stack();

Code error:
You were printing the outputs in the wrong order.
Instead of:

	printPermutation(ans+"(", n ,lpcount+1,rpcount);


	printPermutation(ans+")", n ,lpcount,rpcount+1);

The order of recursive calls should be:

		printPermutation(ans + ")", n, lpcount, rpcount + 1);

		printPermutation(ans + "(", n, lpcount + 1, rpcount);

https://ide.codingblocks.com/s/279516 Alternate approach for reference.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.