-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckParanthesis.java
More file actions
54 lines (52 loc) · 1.24 KB
/
CheckParanthesis.java
File metadata and controls
54 lines (52 loc) · 1.24 KB
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
46
47
48
49
50
51
52
53
54
public class CheckParanthesis {
//link - https://leetcode.com/problems/valid-parentheses/description/
public static void main(String[] args) {
String s = "(){]";
System.out.println(isValid(s));
}
public static boolean isValid(String s) {
char[] check = new char[s.length()+1];
int n=0;
check[0]='.';
for(int i=0;i<s.length();i++)
{
char a = s.charAt(i);
if(a=='(' || a=='{' || a=='[')
{
n++;
check[n] = a;
}
else
{
// if(n==0)
// {
// return true;
// }
if(a==')' && check[n]=='(')
{
n--;
}
else if(a=='}' && check[n]=='{')
{
n--;
}
else if(a==']' && check[n]=='[')
{
n--;
}
else
{
return false;
}
}
}
if(n==0)
{
return true;
}
else
{
return false;
}
}
}