From 4c471b29de4d1f541776aea31eb17148bde0771c Mon Sep 17 00:00:00 2001 From: Satvik Tripathi <113835243+satviktripathi369@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:00:38 +0530 Subject: [PATCH] Create 20ValidParentheses.java Valid Parenthesis solution of the question number 20 on LeetCode. --- .../Leetcode Solutions/20ValidParentheses.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 Hacktoberfest-2023/Leetcode Solutions/20ValidParentheses.java diff --git a/Hacktoberfest-2023/Leetcode Solutions/20ValidParentheses.java b/Hacktoberfest-2023/Leetcode Solutions/20ValidParentheses.java new file mode 100644 index 000000000..55d52d130 --- /dev/null +++ b/Hacktoberfest-2023/Leetcode Solutions/20ValidParentheses.java @@ -0,0 +1,12 @@ +class Solution { + public boolean isValid(String s) { + Stack stack=new Stack(); + for(char c: s.toCharArray()){ + if(c=='(') stack.push(')'); + else if(c=='{') stack.push('}'); + else if(c=='[') stack.push(']'); + else if(stack.isEmpty() || stack.pop() != c) return false; + } + return stack.isEmpty(); + } +}