Answers for "balanced string in java"

1

bracket balancing program in java

import java.util.Scanner;
import java.util.ArrayList;
public class BalanceBracket {
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	System.out.println("enter a string with '(', '{' or '['");
	String s = sc.nextLine();
	String y = isBalanced(s);
	System.out.println(y);
}
static String isBalanced(String s)
{
	String result = "NO";
	ArrayList<String> al = new ArrayList<String>();
	
	for(int i = 0; i < s.length(); i++)
	{
		if(s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[')
        {
			al.add(Character.toString(s.charAt(i)));
		}
		
		else if(s.charAt(i) == ')' || s.charAt(i) == '}' || s.charAt(i) == ']')
		{
			if(al.size() != 0 && al.get(al.size()-1).equals("(") && s.charAt(i) == ')')
			{
				al.remove(al.size()-1);
			}
			else if(al.size() != 0 && al.get(al.size()-1).equals("{") && s.charAt(i) == '}')
			{
				al.remove(al.size()-1);
			}
			else if(al.size() != 0 && al.get(al.size()-1).equals("[") && s.charAt(i) == ']')
			{
				al.remove(al.size()-1);
			}
			else
			{
				return result = "NO";
			}
		}
		
	}
	if(al.isEmpty() == true)
	{
		return result = "YES";
	}
	return result;
}
}
Posted by: Guest on May-04-2020
0

bracket balance java

// for Java

class Parser{
    static HashMap<Character,Character> bracket_map = new HashMap<>();

    static {
        bracket_map.put(')', '(');
        bracket_map.put('}','{');
        bracket_map.put(']','[');
    }
    
    boolean isBalanced(String s){
            
        Stack<Character> stack = new Stack<>();
        char temp;
        for (int i = 0; i < s.length(); i++) {
            temp = s.charAt(i);
            if(temp == '{' || temp == '[' || temp == '('){
                stack.push(temp);
            }else {
                if(stack.isEmpty() || stack.peek() != bracket_map.get(temp)) return false;
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
Posted by: Guest on September-11-2020
0

balanced string in java

public static boolean checkBalanced(String check) {
        Stack<Character> S = new Stack<Character>();
        for(int a = 0; a < check.length(); a++){
            char let = check.charAt(a);
            if(let == '[' || let == '{' || let == '(')
                S.push(let);
            else if(let == ']' || let == '}' || let == ')')
            {
                if(S.empty())
                    return false;
                switch(let)
                {
                    // Opening square brace
                    case ']':
                        if (S.pop() != '[')
                            return false;
                        break;
                    // Opening curly brace
                    case '}':
                        if (S.pop() != '{')
                            return false;
                        break;
                    // Opening paren brace
                    case ')':
                        if (S.pop() != '(')
                            return false;
                        break;
                    default:
                        break;
                }
            }
        }
        if(S.empty())
            return true;
        return false;
    }
Posted by: Guest on October-27-2021
0

balanced string in java

public boolean isBalanced(String string) {
		HashMap<Character, Character> bracketMap = new HashMap<>();
		bracketMap.put('(', ')');
		bracketMap.put('[', ']');
		bracketMap.put('{', '}');
		
		Stack<Character> stack = new Stack<>();
		
		for(int count = 0; count < string.length(); count++) {
			char current = string.charAt(count);
			
			if(bracketMap.keySet().contains(current)) {
				stack.push(current);
			} else if(bracketMap.values().contains(current)) {
				if(!stack.empty() && bracketMap.get(stack.peek()) == current){
					stack.pop();
				} else {
					return false;
				}
			}
		}
		return stack.empty();
	}
Posted by: Guest on October-27-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language