Skip to content

Commit

Permalink
added brackets balanced or not
Browse files Browse the repository at this point in the history
  • Loading branch information
GauravWalia19 committed Dec 23, 2018
1 parent b308375 commit fc97d90
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
73 changes: 73 additions & 0 deletions Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;
public class BracketsBalancedOrNot
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String expression = in.nextLine();
in.close();
if(BalancedOrNot(expression))
{
System.out.println("BRACKET BALANCED");
}
else
{
System.out.println("BRACKET NOT BALANCED");
}
}
private static char reverseBracket(char ch)
{
switch(ch)
{
case ')':
return '(';
case ']':
return '[';
case '}':
return '{';
default:
return '0';
}
}
//this method will throw empty stack exception
private static boolean BalancedOrNot(String expression) throws EmptyStackException
{
Stack<Character> S = new Stack<Character>();
try
{
for(int i=0;i<expression.length();i++)
{
char ch = expression.charAt(i);
if(ch=='(' || ch=='[' || ch=='{')
{
S.push(ch);
}
else if(ch==')' || ch==']' || ch=='}')
{
char rev = reverseBracket(ch);
if(S.empty())
{
throw new EmptyStackException();
}
if(rev == S.peek())
{
S.pop();
}
else
{
return false;
}
}
}
if(S.empty())
{
return true;
}
}
catch(Exception e)
{
System.out.println("STACK IS EMPTY");
}
return false;
}
}
2 changes: 2 additions & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* UNROLLED LINKED LIST
* SKIP LIST
* INBUILT LISTS
* [LINKED LIST]
* [ARRAYLIST](Data-Structures/LISTS/arrayList.java)
* [VECTORS](Data-Structures/LISTS/vectors.java)
* MISC
Expand All @@ -46,6 +47,7 @@
* INBUILT STACKS
* [Stack class](Data-Structures/STACKS/INBUILT-STACK/Stacks.java)
* MISC STACKS
* [Given expression have balanced brackets or not](Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)

#### QUEUES

Expand Down
9 changes: 9 additions & 0 deletions datastructures.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Indexer for Data Structures Lover
* implementation
* [C++](C++/Data-Structures/LISTS/VECTORS/Main.cpp)

#### LINKED LIST

#### LIST PY

* blog
Expand Down Expand Up @@ -154,6 +156,13 @@ Indexer for Data Structures Lover
* implementation
* [C](C/Data-Structures/STACKS/MISC-STACKS/minimum_bracket_reversal_for_balanced_expression.c)

#### Given expression has balanced brackets or not

* blog
* docs
* implementation
* [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)

#### Postfix Evaluation

* blog
Expand Down
4 changes: 3 additions & 1 deletion docs/complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ This page contains the complexities of different algorithms in this repository.
* [UNROLLED LINKED LIST](#unrolled-linked-list)
* [SKIP LIST](#skip-list)
* INBUILT LISTS
* LINKED LIST
* [ARRAYLISTS](#arraylists-(JAVA))
* [VECTORS](#vectors-(C++JAVA))
* MISC
Expand All @@ -93,8 +94,9 @@ This page contains the complexities of different algorithms in this repository.
* [INBUILT STACK](#inbuilt-stack)
* JAVA
* C++
* TWO WAY STACK
* [MISC STACKS](#misc-stacks)
* TWO WAY STACK
* [BracketsBalancedOrNot]
* QUEUES
* SIMPLE QUEUE
* FIXED ARRAY SIMPLE QUEUE
Expand Down

0 comments on commit fc97d90

Please sign in to comment.