File tree 1 file changed +45
-0
lines changed
1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 1. You are given a string exp representing an expression.
2
+ // 2. Assume that the expression is balanced i.e. the opening and closing brackets match with each other.
3
+ // 3. But, some of the pair of brackets maybe extra/needless.
4
+ // 4. You are required to print true if you detect extra brackets and false otherwise.
5
+
6
+ // e.g.'
7
+ // ((a + b) + (c + d)) -> false
8
+ // (a + b) + ((c + d)) -> true
9
+
10
+
11
+ import java .io .*;
12
+ import java .util .*;
13
+
14
+ public class DuplicateBrackets {
15
+
16
+ public static boolean check (String str ){
17
+ Stack <Character > st = new Stack <>();
18
+
19
+ for (int i =0 ;i <str .length ();i ++){
20
+ char ch = str .charAt (i );
21
+ if (ch ==')' ){
22
+ if (st .peek ()=='(' ){
23
+ return true ;
24
+ }else {
25
+ while (st .size ()>0 && st .peek ()!='(' ){
26
+ st .pop ();
27
+ }
28
+ st .pop ();
29
+ }
30
+
31
+ }else {
32
+ st .push (ch );
33
+ }
34
+ // System.out.println(st);
35
+ }
36
+ return false ;
37
+ }
38
+
39
+ public static void main (String [] args ) throws Exception {
40
+ Scanner sc = new Scanner (System .in );
41
+ String str = sc .nextLine ();
42
+ System .out .println (check (str ));
43
+ }
44
+
45
+ }
You can’t perform that action at this time.
0 commit comments