Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trak.parentheses;
- import trak.datastructures.TrakStack;
- /**
- * Class to check whether parentheses in input are valid
- *
- */
- public class CheckParentheses {
- TrakStack stack = new TrakStack();
- // IMPLEMENT THIS CLASS
- public boolean check(String input) {
- for (int i = 0; i < input.length(); i++) {
- char current = input.charAt(i);
- if (current == '(' || current == '[' || current == '{') {
- this.stack.push(current);
- }
- if (current == ')') {
- if (this.stack.isEmpty()) {
- return false;
- } if (this.stack.top().equals('(')) {
- this.stack.pop();
- }
- }else if(current == ']') {
- if (this.stack.isEmpty()) {
- return false;
- } if (this.stack.top().equals('[')) {
- this.stack.pop();
- }
- }else if (current == '}') {
- if (this.stack.isEmpty()) {
- return false;
- } if (this.stack.top().equals('{')) {
- this.stack.pop();
- }
- }
- }if (this.stack.isEmpty()) {
- return true;
- }return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement