Repository: TheAlgorithms/Java
Pull request #3689 (2022-10-26 20:46:21)

Create ValidParentheses.java
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


skinox: Can you reply to this, since I don't really understand how it works, there are a lot of pull requests, however only random one's are chosen and I see there are some pull requests that are 20+ days old/
adjamalbek: > Add some JUnit tests

I did, https://github.com/TheAlgorithms/Java/pull/3709 2 days ago, for all the commits 
adjamalbek: All the errors are fixed
debasishbsws: unable to build. 
adjamalbek: Sorry, I didn't put last commit
adjamalbek: Requesting review from @siriak and @yanglbme and @debasishbsws for approval and merging.
siriak: @debasishbsws please review again

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 38a38cf0..0345ff4e 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -477,6 +477,7 @@
             * [ReverseStringRecursive](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ReverseStringRecursive.java)
             * [Rotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Rotation.java)
             * [Upper](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Upper.java)
+            * [ValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/ValidParentheses.java)
             * [WordLadder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/WordLadder.java)
             * zigZagPattern
               * [zigZagPattern](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/zigZagPattern/zigZagPattern.java)
@@ -648,6 +649,7 @@
             * [ReverseStringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ReverseStringTest.java)
             * [RotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/RotationTest.java)
             * [UpperTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/UpperTest.java)
+            * [ValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java)
             * [WordLadderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/WordLadderTest.java)
             * zigZagPattern
               * [zigZagPatternTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/zigZagPattern/zigZagPatternTest.java)
diff --git a/src/main/java/com/thealgorithms/strings/ValidParentheses.java b/src/main/java/com/thealgorithms/strings/ValidParentheses.java
new file mode 100644
index 00000000..e2b0f8e9
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/ValidParentheses.java
@@ -0,0 +1,33 @@
+package com.thealgorithms.strings;
+//        Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
+//        An input string is valid if:
+//        Open brackets must be closed by the same type of brackets.
+//        Open brackets must be closed in the correct order.
+//        Every close bracket has a corresponding open bracket of the same type.
+
+
+public class ValidParentheses {
+	public static boolean isValid(String s) {
+		char[] stack = new char[s.length()];
+		int head = 0;
+		for(char c : s.toCharArray()) {
+			switch(c) {
+				case '{':
+				case '[':
+				case '(':
+					stack[head++] = c;
+					break;
+				case '}':
+					if(head == 0 || stack[--head] != '{') return false;
+					break;
+				case ')':
+					if(head == 0 || stack[--head] != '(') return false;
+					break;
+				case ']':
+					if(head == 0 || stack[--head] != '[') return false;
+					break;
+			}
+		}
+		return head == 0;
+	}
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java b/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java
new file mode 100644
index 00000000..ab215f22
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java
@@ -0,0 +1,24 @@
+package com.thealgorithms.strings;
+
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+public class ValidParenthesesTest {
+	
+    @Test
+    void testOne() {
+        assertEquals(true, ValidParentheses.isValid("()"));
+    }
+  
+    @Test
+    void testTwo() {
+        assertEquals(true, ValidParentheses.isValid("()[]{}"));
+    }
+  
+  
+    @Test
+    void testThree() {
+        assertEquals(false, ValidParentheses.isValid("(]"));
+    }
+	
+}