Repository: TheAlgorithms/Java
Pull request #5616 (2024-10-06 15:05:39)

Added another method to check valid parentheses in ValidParentheses.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->
Added another method to check valid parentheses in ValidParentheses.java. Also added related testcases.

<!-- 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.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5616?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `81.81818%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 54.76%. Comparing base [(`b190cb7`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b190cb72def8d926e2f73d9f80cfd27c88ff3a3a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`3816c74`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/3816c74f0714ec4365f0612f9796ef29a5b5a093?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5616?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...va/com/thealgorithms/strings/ValidParentheses.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5616?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fstrings%2FValidParentheses.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zdHJpbmdzL1ZhbGlkUGFyZW50aGVzZXMuamF2YQ==) | 81.81% | [0 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5616?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5616      +/-   ##
============================================
+ Coverage     54.75%   54.76%   +0.01%     
- Complexity     3453     3457       +4     
============================================
  Files           538      538              
  Lines         15583    15594      +11     
  Branches       2961     2965       +4     
============================================
+ Hits           8532     8540       +8     
  Misses         6694     6694              
- Partials        357      360       +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5616?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/strings/ValidParentheses.java b/src/main/java/com/thealgorithms/strings/ValidParentheses.java
index f4f3761b..629fee49 100644
--- a/src/main/java/com/thealgorithms/strings/ValidParentheses.java
+++ b/src/main/java/com/thealgorithms/strings/ValidParentheses.java
@@ -38,4 +38,22 @@ public final class ValidParentheses {
         }
         return head == 0;
     }
+    public static boolean isValidParentheses(String s) {
+        int i = -1;
+        char[] stack = new char[s.length()];
+        String openBrackets = "({[";
+        String closeBrackets = ")}]";
+        for (char ch : s.toCharArray()) {
+            if (openBrackets.indexOf(ch) != -1) {
+                stack[++i] = ch;
+            } else {
+                if (i >= 0 && openBrackets.indexOf(stack[i]) == closeBrackets.indexOf(ch)) {
+                    i--;
+                } else {
+                    return false;
+                }
+            }
+        }
+        return i == -1;
+    }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java b/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java
index 22deb4b1..2b6884c9 100644
--- a/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java
+++ b/src/test/java/com/thealgorithms/strings/ValidParenthesesTest.java
@@ -10,15 +10,18 @@ public class ValidParenthesesTest {
     @Test
     void testOne() {
         assertTrue(ValidParentheses.isValid("()"));
+        assertTrue(ValidParentheses.isValidParentheses("()"));
     }
 
     @Test
     void testTwo() {
         assertTrue(ValidParentheses.isValid("()[]{}"));
+        assertTrue(ValidParentheses.isValidParentheses("()[]{}"));
     }
 
     @Test
     void testThree() {
         assertFalse(ValidParentheses.isValid("(]"));
+        assertFalse(ValidParentheses.isValidParentheses("(]"));
     }
 }