Skip to content

Commit 2be06ae

Browse files
authored
Merge pull request #497 from Satvik782/satvik782
Added Java Valid Parentheses
2 parents e221c2d + d93b995 commit 2be06ae

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

java/Valid Parentheses.java

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
4+
ArrayDeque<Character> dq = new ArrayDeque<Character>(s.length());
5+
6+
boolean valid = false;
7+
8+
int i;
9+
for(i=0;i<s.length();i++){
10+
char c = s.charAt(i);
11+
12+
if(c=='(' || c=='{' || c=='['){
13+
dq.add(c);
14+
}
15+
16+
if(dq.isEmpty()){
17+
return true;
18+
}
19+
20+
if(c=='}' && dq.getLast() != '{'){
21+
return false;
22+
}
23+
else{
24+
dq.removeLast();
25+
}
26+
27+
if(c==')' && dq.getLast() != '('){
28+
return false;
29+
}
30+
else{
31+
dq.removeLast();
32+
}
33+
34+
if(c==']' && dq.getLast() != '['){
35+
return false;
36+
}
37+
else{
38+
dq.removeLast();
39+
}
40+
41+
}
42+
43+
44+
45+
return (true);
46+
}
47+
}

0 commit comments

Comments
 (0)