-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValid Parentheses.java
45 lines (38 loc) · 1.16 KB
/
Valid Parentheses.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
38
39
40
41
42
43
44
45
class Solution {
public boolean isValid(String s) {
//If the number of elements in the string equal odd number(3,5.7...)
if(s.length()%2 != 0)
{
return false;
}
//create stack
Stack<Character> stack = new Stack<>();
for(char c : s.toCharArray())
{
//fill the stack with the left part of the parentheses
if(c=='(' || c=='{' || c=='[')
{
stack.push(c);
}
else
{
//check if the stack is empty, so return false
if(stack.isEmpty())
{
return false;
}
//check if the top(peek that i pushed previously) equals the ( and the c or the current element i stand on == ), if true so remove the first parenses on the top
if(c==')' && stack.peek()=='(' || c=='}' && stack.peek()=='{' || c==']' && stack.peek()=='[')
{
stack.pop();
}
else
{
return false;
}
}
}
//if the stack return empty, so each part of the parenthes found the other part
return stack.isEmpty();
}
}