8000 Create 0020_Valid Parantheses.java · berkayzaimdev/LeetCode-Java@1873ea2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1873ea2

Browse files
Create 0020_Valid Parantheses.java
1 parent bf065f7 commit 1873ea2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Solutions/0020_Valid Parantheses.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean isValid(String s)
3+
{
4+
Stack<Character> stack = new Stack<>();
5+
for(char c : s.toCharArray())
6+
{
7+
if(c == '{' || c == '[' || c == '(') stack.push(c);
8+
else
9+
{
10+
if(stack.isEmpty()) return false;
11+
else
12+
{
13+
char top = stack.peek();
14+
if(c == '}' && top == '{' ||
15+
c == ')' && top =='(' ||
16+
c == ']' && top == '[') stack.pop();
17+
else return false;
18+
}
19+
}
20+
}
21+
return stack.isEmpty();
22+
}
23+
}

0 commit comments

Comments
 (0)
0