Day 5:-
Program Statement:-Stacks: Balanced Brackets
On Paper:-
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
/**
* @author vikram.shanbogar@gmail.com
* on 7/30/2017.
*/
public class Stacks {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
String expression = scan.next();
if (expression.length() % 2 != 0) {
strs[i] = "NO";
} else
strs[i] = ((isBalanced(expression)) ? "YES" : "NO");
}
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
static boolean isBalanced(String expression) {
Stack<Character> stack;
boolean result = true;
stack = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
if ((expression.charAt(i) != '{') && (expression.charAt(i) != '(') && (expression.charAt(i) != '[') &&
(expression.charAt(i) != '}') && (expression.charAt(i) != ')') && (expression.charAt(i) != ']')) {
result = false;
break;
}
if (expression.charAt(i) == '{' || expression.charAt(i) == '(' || expression.charAt(i) == '[') {
stack.push(expression.charAt(i));
} else if (!stack.isEmpty()) {
if (stack.peek() == '{' && expression.charAt(i) == '}' || stack.peek() == '[' && expression.charAt(i) == ']'
|| stack.peek() == '(' && expression.charAt(i) == ')') {
stack.pop();
} else {
result = false;
break;
}
} else {
result = false;
break;
}
}
if (!stack.isEmpty())
result = false;
return result;
}
}
Mistakes:-
Program Statement:-Stacks: Balanced Brackets
On Paper:-
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;
/**
* @author vikram.shanbogar@gmail.com
* on 7/30/2017.
*/
public class Stacks {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
String expression = scan.next();
if (expression.length() % 2 != 0) {
strs[i] = "NO";
} else
strs[i] = ((isBalanced(expression)) ? "YES" : "NO");
}
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
}
static boolean isBalanced(String expression) {
Stack<Character> stack;
boolean result = true;
stack = new Stack<>();
for (int i = 0; i < expression.length(); i++) {
if ((expression.charAt(i) != '{') && (expression.charAt(i) != '(') && (expression.charAt(i) != '[') &&
(expression.charAt(i) != '}') && (expression.charAt(i) != ')') && (expression.charAt(i) != ']')) {
result = false;
break;
}
if (expression.charAt(i) == '{' || expression.charAt(i) == '(' || expression.charAt(i) == '[') {
stack.push(expression.charAt(i));
} else if (!stack.isEmpty()) {
if (stack.peek() == '{' && expression.charAt(i) == '}' || stack.peek() == '[' && expression.charAt(i) == ']'
|| stack.peek() == '(' && expression.charAt(i) == ')') {
stack.pop();
} else {
result = false;
break;
}
} else {
result = false;
break;
}
}
if (!stack.isEmpty())
result = false;
return result;
}
}
Mistakes:-
- I was over confident.
- I didn't test enough with different scenarios.
- EmptyStackexception, etc
No comments:
Post a Comment