From c7527927f786927a1070e2ef58df30992900b4a4 Mon Sep 17 00:00:00 2001 From: Stepan Date: Tue, 29 Aug 2023 19:17:46 +0300 Subject: [PATCH 1/3] added validate-parenthesis_en --- problem/validate-parenthesis_en.md | 47 ++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/problem/validate-parenthesis_en.md b/problem/validate-parenthesis_en.md index c4fc2a3d..b4818a18 100644 --- a/problem/validate-parenthesis_en.md +++ b/problem/validate-parenthesis_en.md @@ -1,4 +1,47 @@ +# Valid Parentheses Checker -There is no solution yet. +This code implements a solution to check if a given string containing different types of brackets is valid or not. It uses a stack data structure to maintain the order of opening and closing brackets and ensure their validity. -Would you like to [contribute to the solution](https://github.com/BFEdev/BFE.dev-solutions/blob/main/problem/validate-parenthesis_en.md)? [Contribute guideline](https://github.com/BFEdev/BFE.dev-solutions#how-to-contribute) +You can watch a video explanation of this solution [here](https://youtu.be/lwN4SDGTBaY?si=HTgiwVLHvQjaxMuK). + +## Explanation + +The solution employs a stack to manage the opening and closing brackets. The algorithm iterates through the characters of the input string, and for each character: + +1. If it's an opening bracket, it's pushed onto the stack. +2. If it's a closing bracket, the stack's top element is popped and compared to the expected opening bracket based on the `mapping` dictionary. + - If they don't match, the brackets are not valid. +3. If the character is neither an opening nor a closing bracket, it's ignored. + +After iterating through all characters, the stack should be empty if all brackets are properly matched. Otherwise, it's not a valid string. + +```javascript +function isValid(s) { + const stack = []; // Initialize an empty stack to store opening brackets + const mapping = { ')': '(', '}': '{', ']': '[' }; // Mapping of closing brackets to their corresponding opening brackets + + for (const char of s) { // Iterate through each character in the input string + if (mapping[char]) { // If the character is a closing bracket + const top = stack.pop(); // Pop the top element from the stack (last opening bracket) + if (mapping[char] !== top) { // Check if the closing bracket corresponds to the popped opening bracket + return false; // Brackets are not valid + } + } else { // If the character is an opening bracket + stack.push(char); // Push it onto the stack + } + } + + return stack.length === 0; // If the stack is empty, all brackets are valid; otherwise, the string is not valid +} + +// Test cases +console.log(isValid("()")); // Output: true +console.log(isValid("()[]{}")); // Output: true +console.log(isValid("(]")); // Output: false +``` + +## Time and Space Complexity + +The time complexity of this solution is O(n), where 'n' represents the length of the input string. The algorithm iterates through each character in the string exactly once, and the operations performed within each iteration are constant time operations. + +The space complexity is also O(n). The space used by the stack is proportional to the length of the input string in the worst case, as it stores opening brackets. The mapping dictionary occupies a constant amount of space, irrespective of the input size. From c39b9146e37b5f6c01110303ffdc66d60f0d77e7 Mon Sep 17 00:00:00 2001 From: Stepan Date: Fri, 15 Sep 2023 13:55:19 +0300 Subject: [PATCH 2/3] deleted youtube link --- problem/validate-parenthesis_en.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problem/validate-parenthesis_en.md b/problem/validate-parenthesis_en.md index b4818a18..c3d78011 100644 --- a/problem/validate-parenthesis_en.md +++ b/problem/validate-parenthesis_en.md @@ -2,7 +2,7 @@ This code implements a solution to check if a given string containing different types of brackets is valid or not. It uses a stack data structure to maintain the order of opening and closing brackets and ensure their validity. -You can watch a video explanation of this solution [here](https://youtu.be/lwN4SDGTBaY?si=HTgiwVLHvQjaxMuK). +Created by [@StepanNaryshkov](https://github.com/StepanNaryshkov) ## Explanation From 2de05bef56e6c96d9a07d7579e660546721155b6 Mon Sep 17 00:00:00 2001 From: Stepan Date: Fri, 15 Sep 2023 14:05:04 +0300 Subject: [PATCH 3/3] moved contact to the bottom --- problem/validate-parenthesis_en.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/problem/validate-parenthesis_en.md b/problem/validate-parenthesis_en.md index c3d78011..ea85d008 100644 --- a/problem/validate-parenthesis_en.md +++ b/problem/validate-parenthesis_en.md @@ -2,8 +2,6 @@ This code implements a solution to check if a given string containing different types of brackets is valid or not. It uses a stack data structure to maintain the order of opening and closing brackets and ensure their validity. -Created by [@StepanNaryshkov](https://github.com/StepanNaryshkov) - ## Explanation The solution employs a stack to manage the opening and closing brackets. The algorithm iterates through the characters of the input string, and for each character: @@ -45,3 +43,5 @@ console.log(isValid("(]")); // Output: false The time complexity of this solution is O(n), where 'n' represents the length of the input string. The algorithm iterates through each character in the string exactly once, and the operations performed within each iteration are constant time operations. The space complexity is also O(n). The space used by the stack is proportional to the length of the input string in the worst case, as it stores opening brackets. The mapping dictionary occupies a constant amount of space, irrespective of the input size. + +Created by [@StepanNaryshkov](https://github.com/StepanNaryshkov) \ No newline at end of file