In: Computer Science
WRITE IN JAVA PLEASE~
A string may use more than one type of delimiter to bracket information into "blocks". The primary ways to divide things up into block is by the braces { } , parentheses ( ), and brackets [ ] as delimiters.
CODE:
import java.util.*;
import java.io.*;
public class CheckParanthesis {
public static boolean checkbalance(String str)
{
//Defining a stack.
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<str.length();i++)
{
//taking each character at a time.
char ch=str.charAt(i);
//if the character is anything other than delimiters, we will
ignore it.
if(ch!='(' && ch!='[' && ch!='{' && ch!=')'
&& ch!=']' && ch!='}')
continue;
//if the character is an open delimiter we push it in the
stack.
if(ch=='(' || ch=='[' || ch=='{')
{
stack.push(ch);
continue;
}
//If its not an opening delimiter or a character, it must be a
closing delimiter. And so, there must be an opening delimeter in
the stack.
if(stack.isEmpty())
return false;//if the stack is empty, then there is no opening
delimiter and therefore it is an invalid expression.
if(ch==')' || ch==']' || ch=='}')//if the character is a closing
delimter.
{
char x=stack.pop();//pop the last entered stack value. or the value
in the top of the stack
//if they dont match we return false.
if(ch=='}' && x!='{')
return false;
if(ch==')' && x!='(')
return false;
if(ch==']' && x!='[')
return false;
}
}
return stack.isEmpty();//if the stack is not empty it will return
false, else true.
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a String: ");
String str=in.nextLine();
if(checkbalance(str))
{
System.out.println("good");
}
else
{
System.out.println("bad");
}
}
}
import java.util.*;
import java.io.*;
public class CheckParanthesis {
public static boolean checkbalance(String str)
{
//Defining a stack.
Stack<Character> stack = new Stack<Character>();
for(int i=0;i<str.length();i++)
{
//taking each character at a time.
char ch=str.charAt(i);
//if the character is anything other than delimiters, we will ignore it.
if(ch!='(' && ch!='[' && ch!='{' && ch!=')' && ch!=']' && ch!='}')
continue;
//if the character is an open delimiter we push it in the stack.
if(ch=='(' || ch=='[' || ch=='{')
{
stack.push(ch);
continue;
}
//If its not an opening delimiter or a character, it must be a closing delimiter. And so, there must be an opening delimeter in the stack.
if(stack.isEmpty())
return false;//if the stack is empty, then there is no opening delimiter and therefore it is an invalid expression.
if(ch==')' || ch==']' || ch=='}')//if the character is a closing delimter.
{
char x=stack.pop();//pop the last entered stack value. or the value in the top of the stack
//if they dont match we return false.
if(ch=='}' && x!='{')
return false;
if(ch==')' && x!='(')
return false;
if(ch==']' && x!='[')
return false;
}
}
return stack.isEmpty();//if the stack is not empty it will return false, else true.
}
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a String: ");
String str=in.nextLine();
if(checkbalance(str))
{
System.out.println("good");
}
else
{
System.out.println("bad");
}
}
}
OUTPUT:
If you have any doubts or require any clarifications, please leave a comment and i will answer it. Thank you :)