Skip to content

Commit d2f6422

Browse files
committed
feat
1 parent 5ba8ac8 commit d2f6422

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class ValidParenthese {
2+
private static boolean checkValid(String str) {
3+
Stack<Character> st = new Stack<>();
4+
for (char c : str.toCharArray()) {
5+
if (c == '(') {
6+
st.push(c);
7+
} else if (c == ')') {
8+
if (st.isEmpty()) {
9+
return false;
10+
}
11+
st.pop();
12+
}
13+
}
14+
15+
return st.isEmpty();
16+
}
17+
18+
public static void main(String[] args) {
19+
String str1 = "()", str2 = "()()", str3 = "(()())", str4 = "()())()(())", str5 = ")(()())";
20+
System.out.println("str: " + str1 + " is " + (checkValid(str1) ? "valid" : "invalid" + " parenthese string."));
21+
System.out.println("str: " + str2 + " is " + (checkValid(str2) ? "valid" : "invalid" + " parenthese string."));
22+
System.out.println("str: " + str3 + " is " + (checkValid(str3) ? "valid" : "invalid" + " parenthese string."));
23+
System.out.println("str: " + str4 + " is " + (checkValid(str4) ? "valid" : "invalid" + " parenthese string."));
24+
System.out.println("str: " + str5 + " is " + (checkValid(str5) ? "valid" : "invalid" + " parenthese string."));
25+
}
26+
}

0 commit comments

Comments
 (0)