Doubt regarding Redundant Parenthesis problem

Can you tell me , where did I go wrong in my approach in solving this problem .

Here is the code

import java.util.;
import java.io.
;
public class Main {
public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in)) ;
	int t = Integer.parseInt(br.readLine()) ;
	while(t -- > 0){
		String str = br.readLine() ;
		if(containDuplicates(str)){
			System.out.println("Duplicates") ;
		}
		else{
			System.out.println("Not Duplicates") ;
		}
	}

}

public static boolean containDuplicates(String str){

	LinkedList<Character> stack = new LinkedList<>() ;

	for(int i = 0 ; i < str.length() - 1 ; i ++){

		if(Character.isLetterOrDigit(str.charAt(i))){
			// do nothing
		}

		else if(str.charAt(i) == '(' || str.charAt(i) == '{' || str.charAt(i) == '['){
			stack.push(str.charAt(i)) ;
		}

		else if(str.charAt(i) == ')'){
			while(stack.size() > 0 && stack.peek() != '('){
				stack.pop() ;	// poping all elements
			}
			stack.pop() ;		// poping the '('
		}

		else if(str.charAt(i) == '}'){
			while(stack.size() > 0 && stack.peek() != '{'){
				stack.pop() ;	// poping all elements
			}
			stack.pop() ;		// poping the '{'
		}

		else if(str.charAt(i) == ']'){
			while(stack.size() > 0 && stack.peek() != ']'){
				stack.pop() ;	// poping all elements
			}
			stack.pop() ;		// poping the '['
		}

		else{
			stack.push(str.charAt(i)) ;		// pushing all the operators
		}

	}

	return stack.peek() == '(' || stack.peek() == '{' || stack.peek() == '[' ;		// checking for duplicates
}

}

@Lalit2142
See the redundancy will arise when immediate pop hits an open brace, i.e condition like this () so it is unwanted.

So you have to push the other operands as well as operators, then only you can check if there is any valid expression between a pair of brace, right!

I have corrected your code.

Check it here.

If you have any doubt then ask me else resolve this and rate full.

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.