-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson7_Nesting.java
37 lines (32 loc) · 934 Bytes
/
Lesson7_Nesting.java
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
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
// you can also use imports, for example:
import java.util.*;
import java.lang.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Stack st = new Stack();
for(int i=0;i<S.length();i++){
char c=S.charAt(i);
if( '('==c){
st.push(c);
}
else{
if(st.isEmpty())
return 0;
char p=(char)st.pop();
if(!(c==')' && p=='(') ){
return 0;
}
}
}
if(st.isEmpty())
return 1;
return 0;
}
}