Skip to content

Commit fc97d90

Browse files
committed
added brackets balanced or not
1 parent b308375 commit fc97d90

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import java.util.*;
2+
public class BracketsBalancedOrNot
3+
{
4+
public static void main(String[] args)
5+
{
6+
Scanner in = new Scanner(System.in);
7+
String expression = in.nextLine();
8+
in.close();
9+
if(BalancedOrNot(expression))
10+
{
11+
System.out.println("BRACKET BALANCED");
12+
}
13+
else
14+
{
15+
System.out.println("BRACKET NOT BALANCED");
16+
}
17+
}
18+
private static char reverseBracket(char ch)
19+
{
20+
switch(ch)
21+
{
22+
case ')':
23+
return '(';
24+
case ']':
25+
return '[';
26+
case '}':
27+
return '{';
28+
default:
29+
return '0';
30+
}
31+
}
32+
//this method will throw empty stack exception
33+
private static boolean BalancedOrNot(String expression) throws EmptyStackException
34+
{
35+
Stack<Character> S = new Stack<Character>();
36+
try
37+
{
38+
for(int i=0;i<expression.length();i++)
39+
{
40+
char ch = expression.charAt(i);
41+
if(ch=='(' || ch=='[' || ch=='{')
42+
{
43+
S.push(ch);
44+
}
45+
else if(ch==')' || ch==']' || ch=='}')
46+
{
47+
char rev = reverseBracket(ch);
48+
if(S.empty())
49+
{
50+
throw new EmptyStackException();
51+
}
52+
if(rev == S.peek())
53+
{
54+
S.pop();
55+
}
56+
else
57+
{
58+
return false;
59+
}
60+
}
61+
}
62+
if(S.empty())
63+
{
64+
return true;
65+
}
66+
}
67+
catch(Exception e)
68+
{
69+
System.out.println("STACK IS EMPTY");
70+
}
71+
return false;
72+
}
73+
}

Java/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* UNROLLED LINKED LIST
3333
* SKIP LIST
3434
* INBUILT LISTS
35+
* [LINKED LIST]
3536
* [ARRAYLIST](Data-Structures/LISTS/arrayList.java)
3637
* [VECTORS](Data-Structures/LISTS/vectors.java)
3738
* MISC
@@ -46,6 +47,7 @@
4647
* INBUILT STACKS
4748
* [Stack class](Data-Structures/STACKS/INBUILT-STACK/Stacks.java)
4849
* MISC STACKS
50+
* [Given expression have balanced brackets or not](Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)
4951

5052
#### QUEUES
5153

datastructures.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Indexer for Data Structures Lover
111111
* implementation
112112
* [C++](C++/Data-Structures/LISTS/VECTORS/Main.cpp)
113113

114+
#### LINKED LIST
115+
114116
#### LIST PY
115117

116118
* blog
@@ -154,6 +156,13 @@ Indexer for Data Structures Lover
154156
* implementation
155157
* [C](C/Data-Structures/STACKS/MISC-STACKS/minimum_bracket_reversal_for_balanced_expression.c)
156158

159+
#### Given expression has balanced brackets or not
160+
161+
* blog
162+
* docs
163+
* implementation
164+
* [JAVA](Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java)
165+
157166
#### Postfix Evaluation
158167

159168
* blog

docs/complexity.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This page contains the complexities of different algorithms in this repository.
8383
* [UNROLLED LINKED LIST](#unrolled-linked-list)
8484
* [SKIP LIST](#skip-list)
8585
* INBUILT LISTS
86+
* LINKED LIST
8687
* [ARRAYLISTS](#arraylists-(JAVA))
8788
* [VECTORS](#vectors-(C++JAVA))
8889
* MISC
@@ -93,8 +94,9 @@ This page contains the complexities of different algorithms in this repository.
9394
* [INBUILT STACK](#inbuilt-stack)
9495
* JAVA
9596
* C++
97+
* TWO WAY STACK
9698
* [MISC STACKS](#misc-stacks)
97-
* TWO WAY STACK
99+
* [BracketsBalancedOrNot]
98100
* QUEUES
99101
* SIMPLE QUEUE
100102
* FIXED ARRAY SIMPLE QUEUE

0 commit comments

Comments
 (0)