Valid Parentheses | LeetCode | Java

RMAG news
class Solution {
public boolean isValid(String s) {

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

for(char ch : s.toCharArray()){
if(stack.isEmpty())
stack.add(ch);
else if(!stack.isEmpty()){
if(ch==‘]’ && stack.peek()==‘[‘)
stack.pop();

else if(ch==‘}’ && stack.peek()==‘{‘)
stack.pop();

else if(ch==‘)’ && stack.peek()==‘(‘)
stack.pop();

else
stack.push(ch);
}
}

return stack.isEmpty();
}
}

Thanks for reading 🙂
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
Youtube
Github
Twitter(X)
Medium