bracket balancing program in java
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;
}
}
Also in Java:
- Title
- java treeset
- Category
- Java
- Title
- spigot get player from UUID
- Category
- Java
- Title
- convert java list to array
- Category
- Java
- Title
- java script num toSting syntax eror
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- java identifier expected
- Category
- Java
- Title
- java int to string
- Category
- Java
- Title
- compare string integer java
- Category
- Java
- Title
- java.sql.SQLException: Before start of result set
- Category
- Java
- Title
- java code to concatinate integer
- Category
- Java
- Title
- jquery set data attribute value
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- generate all prime number less than n java
- Category
- Java
- Title
- why python is slower than java
- Category
- Java
- Title
- The shrinker may have failed to optimize the Java bytecode. To disable the shrinker, pass the `--no-shrink` flag to this command.
- Category
- Java
- Title
- Java how to handle HTTP GET request after establishing TCP connection
- Category
- Java
- Title
- array string java in one line
- Category
- Java
- Title
- lastindexof java
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- java initialize int array
- Category
- Java
- Title
- java comparable
- Category
- Java
- Title
- how to select a random element from an array in java
- Category
- Java
- Title
- how to find the intersection of two rectangles in java
- Category
- Java
- Title
- java create jframe
- Category
- Java
- Title
- how to print something in java
- Category
- Java
- Title
- take a peice from array java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- java classes and methods
- Category
- Java
- Title
- java get current milliseconds
- Category
- Java
- Title
- how to pass enum in postman body
- Category
- Java
- Title
- how intent
- Category
- Java
- Title
- displaying an arraylist in java
- Category
- Java
- Title
- Matlab java.lang.IllegalArgumentException: Invalid range
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- java type casting
- Category
- Java
- Title
- how to install java runtime environment on centos 7
- Category
- Java
- Title
- java substring
- Category
- Java
- Title
- how to get multiple integer input in java
- Category
- Java
- Title
- linux pyspark select java version
- Category
- Java
- Title
- remove duplicates from singly linked list
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- write file from a specific location in java
- Category
- Java
- Title
- java foreach
- Category
- Java
- Title
- Spigot API inventory close
- Category
- Java
- Title
- format localdate java
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- close searchview android java
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- java create window
- Category
- Java
- Title
- math.pow java
- Category
- Java
- Title
- get first character of string java
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- update value of an arraylist java
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- java flood fill
- Category
- Java
- Title
- declare class variable in java
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- binary string to int java
- Category
- Java
- Title
- android how to switch between activities
- Category
- Java
- Title
- switch statement in apex
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- sending a excel in an attachment in email java
- Category
- Java
- Title
- arraylist remove method java
- Category
- Java
- Title
- how to show the hex detail of a file in java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- java print type of object
- Category
- Java
- Title
- java variables in annotations
- Category
- Java
- Title
- convert array of int to arraylist java
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- copy array in java
- Category
- Java
- Title
- java switch display panel
- Category
- Java
- Title
- java obtain list string from list object
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- finding min and max from given number in java
- Category
- Java
- Title
- java how to serialize a file into a local server
- Category
- Java
- Title
- binary search tree insert java
- Category
- Java
- Title
- java string to lower case
- Category
- Java
- Title
- java http client eviction
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- java 8 findany on null list
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- summary of operator java
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- why static variables cannot be serialized in java
- Category
- Java
- Title
- java how to put a string sentence in a map
- Category
- Java
- Title
- convert string to localdatetime
- Category
- Java
- Title
- Removing DOM nodes when traversing a NodeList
- Category
- Java
- Title
- get random number from enum in java
- Category
- Java
- Title
- read lines of file randomly java
- Category
- Java