Valid Parenthesis LeetCode

 class Solution {

    public boolean isValid(String str) {

               

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

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

if(stack.isEmpty()){

stack.push(str.charAt(i));

}

else if(stack.peek()=='{' && str.charAt(i)=='}'){

stack.pop();

}

else if(stack.peek()=='[' && str.charAt(i)==']'){

stack.pop();

}

else if(stack.peek()=='(' && str.charAt(i)==')'){

stack.pop();

}

else{

stack.push(str.charAt(i));

}

}

if(stack.isEmpty()){

return true;

}

else{

return false;

}

    }

}

Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks