Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potd - Minimum Add to Make Parentheses Valid #24

Merged
merged 2 commits into from
Oct 9, 2024

Conversation

YashviMehta03
Copy link
Contributor

Description

In this contribution, I solved the problem of Minimum Add to Make Parentheses Valid.

Problem statement:
A parentheses string is valid if and only if:
It is the empty string,
It can be written as AB (A concatenated with B), where A and B are valid strings, or
It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
Return the minimum number of moves required to make s valid.

Fixes:

My code beats 100% in time complexity.

Type of Change

  • Question Added
  • Solution Added
  • Other (please specify):

How to Test

Run the code for the different test cases

Checklist

  • I have performed a self-review of my code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have made corresponding changes to the documentation (if applicable).
  • My changes generate no new warnings.
  • I have added tests to cover my changes (if applicable).
  • All new and existing tests pass.

Additional Notes

Alternative solution to the problem is using a stack which increases the space complexity to O(n) . When we encounter an open bracket, we push it onto the stack. When we encounter a close bracket we pop the open if the stack is not empty. But if its empty we increment our moves. In the end after iterating though the string if some open brackets still remain on the stack we add that count to moves. That gives our final answer

@Gyanthakur Gyanthakur self-requested a review October 9, 2024 17:03
@Gyanthakur Gyanthakur merged commit 8a04c37 into Gyanthakur:main Oct 9, 2024
2 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants