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

Remove Outermost Parentheses

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

Depth returns to 0 ⇒ a primitive ends.

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

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

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

Intuition

A primitive part ends each time the open-bracket count drops back to zero. Split the string at those points and strip the first and last character of each part.

Approach

  1. 1

    Track depth with a stack; record indices where it becomes empty.

  2. 2

    Slice the string into primitive parts.

  3. 3

    Drop each part's outer pair and join.

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

Split at depth zeroPythonO(n) time
class Solution:
    def removeOuterParentheses(self, s: str) -> str:
        #capture the index when stack got empty
        indexes=[]
        stack=[]
        for i in range(len(s)):
            if s[i]=="(":
                stack.append(s[i])
            else:
                stack.pop()
            if len(stack)==0:
               indexes.append(i)
        parts=[]
        i=0
        #0:5+1
        #5+1:9+1
        #
        for index in indexes:
            parts.append(s[i:index+1])
            i=index+1
        res=[]
        for part in parts:
            res.append(part[1:len(part)-1])
        return "".join(res)
            
            
       
স্টেপ-থ্রু লোড হচ্ছে…

Remove Outermost Parentheses — সাধারণ প্রশ্ন

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

A primitive part ends each time the open-bracket count drops back to zero. Split the string at those points and strip the first and last character of each part.

Remove Outermost Parentheses (LeetCode 1021) কীভাবে সমাধান করবেন?

1. Track depth with a stack; record indices where it becomes empty. 2. Slice the string into primitive parts. 3. Drop each part's outer pair and join.

Remove Outermost Parentheses-এর time আর space complexity কত?

Time: O(n)। Space: O(n)। (Accepted approach: Split at depth zero।)

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

A depth counter alone (no stack) is enough and avoids extra memory.