01 Jan 2024C++ / Python / TypeScriptEasy

valid parentheses

valid parentheses

LeetCode 20: Valid Parentheses

i recently solved the valid parentheses problem on leetcode, and it's a great example of string, stack techniques. this easy problem tests your understanding of string, stack.

Problem Statement

[problem description would go here - you can add the actual problem statement]

My Approach

when i first saw this problem, i immediately thought about using stack. the key insight is [to be filled based on the specific problem].

Initial Thoughts

i started by thinking about different approaches:

  • **brute force** - [brute force approach description]
  • **optimized approach** - Stack
  • **alternative approach** - [alternative approach if applicable]

Solution Strategy

i decided to use a Stack with the following strategy:

  • **[step 1]** - [description]
  • **[step 2]** - [description]
  • **[step 3]** - [description]

My Solution

def solution(self, params):
# implementation would go here
pass

Code Breakdown

let me walk through how this solution works:

1. [Section 1]

# code snippet

[explanation]

2. [Section 2]

# code snippet

[explanation]

Example Walkthrough

let's trace through an example:

[step-by-step walkthrough]

Time and Space Complexity

  • **time complexity:** [complexity analysis]
  • **space complexity:** [complexity analysis]

Key Insights

  • **[insight 1]** - [explanation]
  • **[insight 2]** - [explanation]
  • **[insight 3]** - [explanation]

Alternative Approaches

i also considered:

  • **[alternative 1]** - [description]
  • **[alternative 2]** - [description]

Edge Cases to Consider

  • **[edge case 1]** - [description]
  • **[edge case 2]** - [description]
  • **[edge case 3]** - [description]

Lessons Learned

this problem taught me:

  • [lesson 1]
  • [lesson 2]
  • [lesson 3]

Conclusion

the valid parentheses problem is a great exercise in string, stack. the key insight is [key takeaway].

you can find my complete solution on [leetcode](https://leetcode.com/problems/valid-parentheses/solutions/1234569/valid-parentheses-solution-by-1cbyc).

---

*this is part of my leetcode problem-solving series. i'm documenting my solutions to help others learn and to track my own progress.*

Solution files

Pythonvalid-parentheses/synced-solution.py
class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        pairs = {class="syntax-string">')':class="syntax-string">'(', class="syntax-string">']':class="syntax-string">'[', class="syntax-string">'}':class="syntax-string">'{'}
        for c in s:
            if c in class="syntax-string">'([{': stack.append(c)
            elif not stack or stack[-class="syntax-number">1] != pairs[c]: return False
            else: stack.pop()
        return not stack
C++valid-parentheses/synced-solution.cpp
class Solution {
public:
    bool isValid(string s) {
        stack<char> st;
        for (char c : s) {
            if (c==class="syntax-string">'(' || c==class="syntax-string">'[' || c==class="syntax-string">'{') st.push(c);
            else {
                if (st.empty()) return false;
                if (c==class="syntax-string">')' && st.top()!=class="syntax-string">'(') return false;
                if (c==class="syntax-string">']' && st.top()!=class="syntax-string">'[') return false;
                if (c==class="syntax-string">'}' && st.top()!=class="syntax-string">'{') return false;
                st.pop();
            }
        }
        return st.empty();
    }
};
TypeScriptvalid-parentheses/synced-solution.ts
function isValid(s: string): boolean {
    const stack: string[] = [];
    const pairs: {[k:string]:string} = {class="syntax-string">')':class="syntax-string">'(', class="syntax-string">']':class="syntax-string">'[', class="syntax-string">'}':class="syntax-string">'{'};
    for (const c of s) {
        if (class="syntax-string">'([{'.includes(c)) stack.push(c);
        else if (!stack.length || stack[stack.length-class="syntax-number">1] !== pairs[c]) return false;
        else stack.pop();
    }
    return stack.length === class="syntax-number">0;
}