We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Data Structures
- Java Stack
- Discussions
Java Stack
Java Stack
Sort by
recency
|
636 Discussions
|
Please Login in order to post a comment
Please this my implementation if anyone note vulnerability please told me
import java.util.*; class Solution{ public static boolean check(String s) { Stack stack = new Stack<>(); for (char ch : s.toCharArray()) {
if (ch == '(' || ch == '{' || ch == '[') { stack.push(ch); } else if (ch == ')' || ch == '}' || ch == ']') { if (stack.isEmpty()) return false; char top = stack.pop(); if ((ch == ')' && top != '(') || (ch == '}' && top != '{') || (ch == ']' && top != '[')) { return false; } } } return stack.isEmpty(); }
}
Java 15+ (using my own Stack class, I forgot that already exists this class :P)