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
}
}