Skip to content

Commit d7cc82a

Browse files
added method in valid parentheses
1 parent b190cb7 commit d7cc82a

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,63 @@
11
package com.thealgorithms.strings;
22
// Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine
3+
34
// if the input string is valid. An input string is valid if: Open brackets must be closed by
45
// the same type of brackets. Open brackets must be closed in the correct order. Every close
56
// bracket has a corresponding open bracket of the same type.
67

78
public final class ValidParentheses {
89
private ValidParentheses() {
910
}
11+
1012
public static boolean isValid(String s) {
1113
char[] stack = new char[s.length()];
1214
int head = 0;
1315
for (char c : s.toCharArray()) {
1416
switch (c) {
15-
case '{':
16-
case '[':
17-
case '(':
18-
stack[head++] = c;
19-
break;
20-
case '}':
21-
if (head == 0 || stack[--head] != '{') {
22-
return false;
23-
}
24-
break;
25-
case ')':
26-
if (head == 0 || stack[--head] != '(') {
27-
return false;
28-
}
29-
break;
30-
case ']':
31-
if (head == 0 || stack[--head] != '[') {
17+
case '{':
18+
case '[':
19+
case '(':
20+
stack[head++] = c;
21+
break;
22+
case '}':
23+
if (head == 0 || stack[--head] != '{') {
24+
return false;
25+
}
26+
break;
27+
case ')':
28+
if (head == 0 || stack[--head] != '(') {
29+
return false;
30+
}
31+
break;
32+
case ']':
33+
if (head == 0 || stack[--head] != '[') {
34+
return false;
35+
}
36+
break;
37+
default:
38+
throw new IllegalArgumentException("Unexpected character: " + c);
39+
}
40+
}
41+
return head == 0;
42+
}
43+
44+
public static boolean isValidParentheses(String s) {
45+
int i = -1;
46+
char[] stack = new char[s.length()];
47+
String openBrackets = "({[";
48+
String closeBrackets = ")}]";
49+
50+
for (char ch : s.toCharArray()) {
51+
if (openBrackets.indexOf(ch) != -1) {
52+
stack[++i] = ch;
53+
} else {
54+
if (i >= 0 && openBrackets.indexOf(stack[i]) == closeBrackets.indexOf(ch)) {
55+
i--;
56+
} else {
3257
return false;
3358
}
34-
break;
35-
default:
36-
throw new IllegalArgumentException("Unexpected character: " + c);
3759
}
3860
}
39-
return head == 0;
61+
return i == -1;
4062
}
4163
}

src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ public class ValidParenthesesTest {
1010
@Test
1111
void testOne() {
1212
assertTrue(ValidParentheses.isValid("()"));
13+
assertTrue(ValidParentheses.isValidParentheses("()"));
1314
}
1415

1516
@Test
1617
void testTwo() {
1718
assertTrue(ValidParentheses.isValid("()[]{}"));
19+
assertTrue(ValidParentheses.isValidParentheses("()[]{}"));
1820
}
1921

2022
@Test
2123
void testThree() {
2224
assertFalse(ValidParentheses.isValid("(]"));
25+
assertFalse(ValidParentheses.isValidParentheses("(]"));
2326
}
2427
}

0 commit comments

Comments
 (0)