Tensorum
YouTubeশেখা শুরু করুন
LeetCode 20 · Parentheses / Bracket Matching

Valid Parentheses

LeetCode 20 · সহজ · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু

Push openers; each closer must match the top.

সহজStringStackBracket Sequences
LeetCode-এ খুলুন ↗

আমি Valid Parentheses (LeetCode 20) এভাবে সমাধান করেছি — Parentheses / Bracket Matching প্যাটার্নের একটি সহজ প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।

নিচের বিবরণ ইংরেজিতে।

Intuition

Every closing bracket must match the most recent unmatched opening bracket — that's last-in-first-out, a stack.

Approach

  1. 1

    Opening bracket → push.

  2. 2

    Closing bracket → pop and check it matches; empty stack → invalid.

  3. 3

    Valid only if the stack is empty at the end.

আমি কীভাবে সমাধান করেছি

Stack matchingPythonO(n) time · O(n) space
class Solution:
    def isValid(self, s: str) -> bool:
        stack = []
        mpp={
            "(":")",
            "{":"}",
            "[":"]"
        }
        stack = []
        for char in s:
            if char in mpp:
                stack.append(char)
            else:
                closing_brace = char
                if stack:
                    opening_brace = stack.pop()
                    if mpp[opening_brace]!=closing_brace:
                        return False
                else:
                    return False
        return True if len(stack)==0 else False
স্টেপ-থ্রু লোড হচ্ছে…

Valid Parentheses — সাধারণ প্রশ্ন

Valid Parentheses-এর পেছনে intuition কী?

Every closing bracket must match the most recent unmatched opening bracket — that's last-in-first-out, a stack.

Valid Parentheses (LeetCode 20) কীভাবে সমাধান করবেন?

1. Opening bracket → push. 2. Closing bracket → pop and check it matches; empty stack → invalid. 3. Valid only if the stack is empty at the end.

Valid Parentheses-এর time আর space complexity কত?

Time: O(n)। Space: O(n)। (Accepted approach: Stack matching।)

Valid Parentheses-এ সাধারণ ভুলগুলো কী?

Leftover openers like '((' make it invalid.