-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesis.java
More file actions
43 lines (35 loc) · 881 Bytes
/
Parenthesis.java
File metadata and controls
43 lines (35 loc) · 881 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
38
39
40
41
42
43
import java.util.*;
class Solution {
boolean solution(String s) {
Queue<Character> queue = new LinkedList<>();
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == '(') queue.add(')');
else{
if(queue.isEmpty()) return false;
else queue.poll();
}
}
return queue.isEmpty();
}
}
/*
class Solution {
boolean solution(String s) {
int cnt=0;
for(int i=0; i<s.length(); i++){
if(s.charAt(i) == ')'){
while(i<s.length() && s.charAt(i) == ')'){
cnt --;
i++;
}
i--;
}else {
cnt ++;
continue;
}
if(cnt < 0) return false;
}
return (cnt == 0) ? true : false;
}
}
*/