Usman Hasan
Usman Hasan

Reputation: 19

Balanced Symbol Check using Stack in Java

Hey guys so I have an assignment where Im supposed to read a string that the user inputs and check for balanced symbols using a stack. So if the string is "{[()]}" the string is balanced because there is a close for every opening. My thought is to use a loop that checks every character from the string that is given and if the string has an opener such as "([{" then it does stack.push(char) and if the character is a closer ")]}" then I need to use stack.pop(char). The issue im running into is that my professor is forcing me to use a string method and any help ive found online is using a boolean method, id appreciate it if someone could help me out here.

I understand that my code is not working but you can at least get the idea of what my logic is.

import java.util.*;

public class BalancedSymbols {

public static String balancedSymbols(String lineToCheck){ //this is the method that im being forced to use

    Stack<String> stack = new Stack<String>();

   for (int i = 0; i<lineToCheck.length(); i++){

        char x = '(';
        char y = '{';
        char z = '[';

        char a;
        a = lineToCheck.charAt(i);

        if (a == x){

            stack.push(a);

        }

        if (a == y){

            stack.push(a);

        }

        if (a == z){

            stack.push(a);

        }


    }

}

}

Obviously I would do the same for popping except with different symbols.

Upvotes: 0

Views: 3964

Answers (2)

Ban
Ban

Reputation: 349

 Create empty stack
 ==========================

private static boolean isValideEx(String str) {

    Stack<Character> st=new Stack<Character>();

    if(str == null || str.length() == 0)
        return true;

    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i)==')'){
                if(!st.isEmpty() && st.peek()=='('){
                    st.pop();
                }else{
                    return false;
                }
        }else if(str.charAt(i)==']'){
                if(!st.isEmpty() && st.peek()=='['){
                    st.pop();
                }else{
                    return false;
                }
        }else if(str.charAt(i)=='}'){
                if(!st.isEmpty() && st.peek()=='{'){
                    st.pop();
                }else{
                    return false;
                }
        }else{
            st.push(str.charAt(i));
        }

    }
    System.out.println(" sy "+st);

    if(st.isEmpty())
        return true;
    else
        return false;
}

Upvotes: 0

Suraj Dubey
Suraj Dubey

Reputation: 536

Here is the logic:

Create empty stack. (you already have done it).

Traverse through the String.

For every character ch you encounter, if ch is } ] ) and stack is empty return false
else

if it is ( { [ push it in stack
if ch is ) } ] check stack top.
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 false
else return true.
The reason behind returning false is we don't have corresponding matching parenthesis for this. These are extra brackets.

Try to implement it on your own first. If you face any problem, comment it.. I'll be happy to help.

Please comment if you have any questions.

Upvotes: 0

Related Questions