-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBalancedParanthesis.java
More file actions
80 lines (47 loc) · 1.07 KB
/
BalancedParanthesis.java
File metadata and controls
80 lines (47 loc) · 1.07 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
Given a string A consisting only of '(' and ')'.
You need to find whether parantheses in A is balanced or not ,if it is balanced then return 1 else return 0.
Problem Constraints
1 <= |A| <= 105
Input Format
First argument is an string A.
Output Format
Return 1 if parantheses in string are balanced else return 0.
Example Input
Input 1:
A = "(()())"
Input 2:
A = "(()"
Example Output
Output 1:
1
Output 2:
0
Example Explanation
Explanation 1:
Given string is balanced so we return 1
Explanation 2:
Given string is not balanced so we return 0
**/
public class Solution {
public int solve(String A) {
Stack<Character> stack = new Stack<Character>();
int i = 0;
while(i < A.length())
{
if(A.charAt(i) == '(')
{
stack.push('(');
}
else
{
if(stack.isEmpty()){
return 0;
}
stack.pop();
}
i++;
}
return stack.isEmpty() ? 1 : 0;
}
}