Skip to content

Commit c8e5e2e

Browse files
committed
ValidParenthesis (){}[]
1 parent 5d27ae2 commit c8e5e2e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/com/leetcode/solution/IntToRoman.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ public int romanToInt(String s) {
4949

5050

5151
}
52+
53+
5254
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.leetcode.solution;
2+
3+
4+
5+
import java.util.*;
6+
7+
public class ValidParenthesis {
8+
public static void main(String[] args) {
9+
10+
11+
String str="()";
12+
13+
Solutions ss= new Solutions();
14+
boolean b=ss.isValid(str);
15+
16+
System.out.println("Given parenthesis is: " + b);
17+
18+
19+
}
20+
}
21+
class Solutions {
22+
public boolean isValid(String s) {
23+
24+
25+
Stack<Character> stack = new Stack<>();
26+
for (char c : s.toCharArray()) {
27+
if (c == '(')
28+
stack.push(')');
29+
else if (c == '{')
30+
stack.push('}');
31+
else if (c == '[')
32+
stack.push(']');
33+
else if (stack.isEmpty() || stack.pop() != c)
34+
return false;
35+
}
36+
return stack.isEmpty();
37+
}
38+
39+
40+
41+
}

0 commit comments

Comments
 (0)