-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedParantheses.java
More file actions
37 lines (31 loc) · 941 Bytes
/
BalancedParantheses.java
File metadata and controls
37 lines (31 loc) · 941 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
35
36
37
/**
* Created by Vatsal Gosaliya on 15-Jul-16.
*
* PROBLEM: Check if the given string contains a balanced pattern of parantheses.
*/
import java.util.Stack;
class BalancedParantheses {
public boolean solution(String a){
int size = a.length();
int check = 0;
Stack s = new Stack();
for(int i=0;i<size;i++){
if(a.charAt(i)!='(' && a.charAt(i)!=')') check = -1;
if(a.charAt(i)=='(') s.push(a.charAt(i));
else{
if(!s.empty()) s.pop();
else return false;
}
}
if(check==-1) return false;
else{
if(s.empty()) return true;
else return false;
}
}
public static void main(String[] args) {
String a = "((()))";
boolean result = new BalancedParantheses().solution(a);
System.out.println(result);
}
}