Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package trak.parentheses;
  2.  
  3.  
  4.  
  5. import trak.datastructures.TrakStack;
  6.  
  7. /**
  8.  * Class to check whether parentheses in input are valid
  9.  *
  10.  */
  11.  
  12. public class CheckParentheses {
  13.     TrakStack stack = new TrakStack();
  14.  
  15.     // IMPLEMENT THIS CLASS
  16.  
  17.     public boolean check(String input) {
  18.         for (int i = 0; i < input.length(); i++) {
  19.             char current = input.charAt(i);
  20.             if (current == '(' || current == '[' || current == '{') {
  21.                 this.stack.push(current);
  22.             }
  23.                 if (current == ')') {
  24.                     if (this.stack.isEmpty()) {
  25.                         return false;
  26.                     } if (this.stack.top().equals('(')) {
  27.                         this.stack.pop();
  28.                    
  29.                     }
  30.                 }else if(current == ']') {
  31.                         if (this.stack.isEmpty()) {
  32.                             return false;
  33.                         } if (this.stack.top().equals('[')) {
  34.                             this.stack.pop();
  35.                         }
  36.    
  37.         }else if (current == '}') {
  38.             if (this.stack.isEmpty()) {
  39.                 return false;
  40.             } if (this.stack.top().equals('{')) {
  41.                 this.stack.pop();
  42.         }
  43.        
  44.  
  45.    
  46.    
  47.     }
  48.     }if (this.stack.isEmpty()) {
  49.         return true;
  50.     }return false;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement