Skip to content

Commit 5ca50f1

Browse files
authored
Create Solution.java (Tahanima#91)
1 parent a9333c5 commit 5ca50f1

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public boolean isValid(String s) {
3+
Stack<Character> st = new Stack<Character>();
4+
5+
for (int i = 0; i < s.length(); i++) {
6+
if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{') {
7+
st.push(s.charAt(i));
8+
} else {
9+
char c;
10+
11+
if (st.size() > 0)
12+
c = st.pop();
13+
else
14+
return false;
15+
16+
if (s.charAt(i) == ']' && c != '[')
17+
return false;
18+
19+
if (s.charAt(i) == '}' && c != '{')
20+
return false;
21+
22+
if (s.charAt(i) == ')' && c != '(')
23+
return false;
24+
}
25+
}
26+
27+
if (st.size() > 0)
28+
return false;
29+
30+
return true;
31+
}
32+
}

0 commit comments

Comments
 (0)