Coding Challenge Day-30: Problem 1: Check if parenthesis in expression are balanced

Hello! So I’m doing a 30 day coding challenge where I solve a few questions every day and thought of posting them here on my blog so that you guys can join the challenge too!

Welcome to Coding challenge Day 30: Problem 1! Be sure to post your answers, queries etc in the comments!

Problem: Check if parenthesis in expression are balanced

Sample input: (A+B) + (x+y)
output: Expression is balanced

Sample input: (A+B) + (x+y)}
output: Expression is not balanced

Solution:

Github: (Link)

package stacks;

public class Symbol_checker {
	
	static class Stack{
		int MAX= 10;
		char [] stack= new char[MAX];
		int top=-1;
		
		public void push(char element){
			if(top<MAX-1){
				top++;
				stack[top]=element;
			} else {
				System.out.println("Stack is Full");
			}
		}
		
		public Character pop (){
			if(top==-1){
				//System.out.println("Stack is empty");
				return null;
			} 
			
			return stack[top--];
		}
	}
	
	public static void main (String [] args){
		Stack obj= new Stack ();
		String exp="(A+B) + (x+y)}";
		boolean balance=true;
		Character ch;
		
		for (int i=0; i<exp.length(); i++){
			char c= exp.charAt(i);
			if( c=='(' || c=='{' || c=='['){
				obj.push(c);
			}
			
			if(c==')' || c=='}' || c== ']'){
				ch= obj.pop();
				if (ch==null){
					balance=false;
					break;
				}
				
				if(c==')' && ch!='('){
					balance=false;
					break;
				}
				
				if(c=='}' && ch!='{'){
					balance=false;
					break;
				}
				if(c==']' && ch!='['){
					balance=false;
					break;
				}
			}
		}
		
		if(obj.pop()!=null){
			balance=false;
		}
		
		if(balance){
			System.out.println("Expression is balanced");
		} else{
			System.out.println("Expression is not balanced");
		}
		
	}

}

Happy Learning!!

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: