Problem Statement
Pattern:
Solution
static boolean ispar(String str) {
// add your code here
Deque<Character> stack = new LinkedList<>();
for (char ch : str.toCharArray()) {
if (ch == '{' || ch == '(' || ch == '[') stack.push(ch);
else {
if (stack.isEmpty()) return false;
char pop = stack.pop();
if (ch == '}' && pop != '{') return false;
if (ch == ')' && pop != '(') return false;
if (ch == ']' && pop != '[') return false;
}
}
return stack.isEmpty();
}
TC : SC :