From cf8ee18867ae3ae1c3d1aeafd062309954e0932b Mon Sep 17 00:00:00 2001 From: Shardul Negi Date: Fri, 29 Oct 2021 23:12:51 +0530 Subject: [PATCH] Create DuplicateBrackets.java 1. You are given a string exp representing an expression. 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each other. 3. But, some of the pair of brackets maybe extra/needless. 4. You are required to print true if you detect extra brackets and false otherwise. e.g.' ((a + b) + (c + d)) -> false (a + b) + ((c + d)) -> true --- DataStructures/Stacks/DuplicateBrackets.java | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 DataStructures/Stacks/DuplicateBrackets.java diff --git a/DataStructures/Stacks/DuplicateBrackets.java b/DataStructures/Stacks/DuplicateBrackets.java new file mode 100644 index 000000000000..dc25d4ea5cbb --- /dev/null +++ b/DataStructures/Stacks/DuplicateBrackets.java @@ -0,0 +1,45 @@ +// 1. You are given a string exp representing an expression. +// 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each other. +// 3. But, some of the pair of brackets maybe extra/needless. +// 4. You are required to print true if you detect extra brackets and false otherwise. + +// e.g.' +// ((a + b) + (c + d)) -> false +// (a + b) + ((c + d)) -> true + + +import java.io.*; +import java.util.*; + +public class DuplicateBrackets { + + public static boolean check(String str){ + Stack st = new Stack<>(); + + for(int i=0;i0 && st.peek()!='('){ + st.pop(); + } + st.pop(); + } + + }else{ + st.push(ch); + } + // System.out.println(st); + } + return false; + } + + public static void main(String[] args) throws Exception { + Scanner sc = new Scanner(System.in); + String str = sc.nextLine(); + System.out.println(check(str)); + } + +}