Skip to content

Add Python solution for Valid Parentheses (Closes #317)#412

Open
maitriupadhyay03-cell wants to merge 3 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:feat/add-valid-parentheses-python
Open

Add Python solution for Valid Parentheses (Closes #317)#412
maitriupadhyay03-cell wants to merge 3 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:feat/add-valid-parentheses-python

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

Description

This PR adds a Python solution for the Valid Parentheses problem using a stack-based approach.

Closes #317

Problem

Given a string s containing just the characters (, ), {, }, [ and ], determine if the input string is valid.

A string is valid if:

  1. Open brackets must be closed by the same type of brackets.
    1. Open brackets must be closed in the correct order.
    1. Every close bracket has a corresponding open bracket of the same type.

Approach

  • Use a stack (list) to track unmatched opening brackets.
    • For each character in the string:
    • If it's an opening bracket (, {, [ → push onto the stack.
    • If it's a closing bracket ), }, ] → check if the top of the stack is the matching opener.
  • - If yes → pop the stack.
    
  • - If no (or stack is empty) → return `False`.
    
    • At the end, the string is valid only if the stack is empty.

Complexity

Complexity
Time O(n) — single pass through the string
Space O(n) — stack stores at most n/2 brackets

Test Cases

Input Expected Output
"()" True
"()[]{}" True
"(]" False
"([)]" False
"{[]}" True
"" True
"{" False

Implemented a function to find the longest common prefix among an array of strings using horizontal scanning. Added test cases to validate the functionality.
Implement Kadane's Algorithm to find the maximum sum of a contiguous subarray with test cases.
Add a Python solution for the Valid Parentheses problem using a stack-based approach.

- Time Complexity: O(n)
- Space Complexity: O(n)
- Includes 7 test cases covering all edge cases

Closes saloni-jaiswal-dev#317
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.

Valid paranthesis problem

1 participant