This repository was archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
added validate-parenthesis_en #62
Open
StepanNaryshkov
wants to merge
3
commits into
BFEdev:main
Choose a base branch
from
StepanNaryshkov:snaryshkov/validate-parenthesis_en
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.