Stack

Balance symbol using Stack

Pinterest LinkedIn Tumblr

Data structure Description

Write a program to read the symbol string from the user. The program should validate the symbols using the stack. Stack uses push and pop operations for validating the symbol.

Code editor validates the syntax and gives error based on the symbol balancing.

Example
{[()]} – Balanced symbol.
{(]} – Not balanced symbol

Algorithm Explanation

Take each character from symbol string.
If the symbol is left parenthesis, brace or bracket push to stack.
If the symbol is the right parenthesis, brace, and bracket, it check the stack.
If the stack is empty, return false.If the stack is non-empty, pop and compare with the left parenthesis, brace, and bracket. If it is not left parenthesis, return false.
If stack is empty return true;

Source Code

package com.dsacode.DataStructre.stack;
 
import java.util.Stack;
 
public class BalanceSymbol {
 
        private static final char L_PAREN    = '(';
        private static final char R_PAREN    = ')';
         
        private static final char L_BRACE    = '{';
        private static final char R_BRACE    = '}';
         
        private static final char L_BRACKET  = '[';
        private static final char R_BRACKET  = ']';
        private static Stack < Character > stack = new Stack < Character >();
         
        public static boolean isBalanced(String str){
             
             
             for (int i = 0; i < str.length(); i++) {
                 Character c = str.charAt(i);
                  
                 if( c == L_PAREN)  
                     stack.push(L_PAREN);
                 else if (c == L_BRACE)  
                     stack.push(L_BRACE);
                 else if (c == L_BRACKET)
                     stack.push(L_BRACKET);
 
                 else if (c == R_PAREN) {
                     if (stack.isEmpty())       
                         return false;
                     if (stack.pop() != L_PAREN)
                         return false;
                 }
                 else if (c == R_BRACE) {
                     if (stack.isEmpty())
                         return false;
                     if (stack.pop() != L_BRACE)
                         return false;
                 }
 
                 else if (c == R_BRACKET) {
                     if (stack.isEmpty())       
                         return false;
                     if (stack.pop() != L_BRACKET)
                         return false;
                 }
             }
                 
             return stack.isEmpty();
            }
        
        public static void main(String[] args) {
            String bsy1="{()){}";
            String bsy2="{(){}}";
            System.out.println("symbol for validating the balance:" + bsy1 +" \n" +"Result:"+ isBalanced(bsy1));
            System.out.println("symbol for validating the balance"  + bsy2 +" \n" +"Result:"+ isBalanced(bsy2));
 
        }
 
    }
 

Output

[(12*45)]{12+45}
true
 
{(34*56)+23(45)
False

Source Explanation

If it is ( { [ symbol, push it in stack.
If character is) } ], check stack top and validate.
If stack top is equal to corresponding opening bracket pop from stack else return false.If you have reached to end of String and stack is not empty return falseelse return true.

Write A Comment