From fc97d9071208bc04605718299ae8562f1eb37d27 Mon Sep 17 00:00:00 2001 From: GauravWalia19 Date: Mon, 24 Dec 2018 00:04:21 +0530 Subject: [PATCH] added brackets balanced or not --- .../MISC-STACKS/BracketsBalancedOrNot.java | 73 +++++++++++++++++++ Java/README.md | 2 + datastructures.md | 9 +++ docs/complexity.md | 4 +- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java diff --git a/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java b/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java new file mode 100644 index 0000000..3c0e7c8 --- /dev/null +++ b/Java/Data-Structures/STACKS/MISC-STACKS/BracketsBalancedOrNot.java @@ -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 S = new Stack(); + try + { + for(int i=0;i