-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedBrackets.java
More file actions
34 lines (30 loc) · 864 Bytes
/
BalancedBrackets.java
File metadata and controls
34 lines (30 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package Data_Structure_And_Algorithm;
import java.util.Scanner;
import java.util.Stack;
public class BalancedBrackets {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
String st = scanner.nextLine();
System.out.println(check(st));
}
public static boolean check(String st){
Stack<Character> stack = new Stack<>();
for (int i=0 ;i < st.length();i++){
char ch = st.charAt(i);
if(ch == ')'){
if(stack.peek()=='('){
return true;
}else{
while (stack.peek()!='('){
stack.pop();
}
stack.pop();
}
}else{
stack.push(ch);
}
}
return false;
}
}