Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 678 · Parentheses / Bracket Matching

Valid Parenthesis String

LeetCode 678 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Two counters: optimistic opens forward, optimistic closes backward.

நடுத்தரம்StringDynamic ProgrammingStackGreedyBracket Sequences
LeetCode இல் திற ↗

நான் Valid Parenthesis String (LeetCode 678) ஐ இப்படித் தீர்த்தேன் — Parentheses / Bracket Matching பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

Scan left to right treating every '*' as '(' — if the open count ever goes negative, there are too many ')'. Scan right to left treating every '*' as ')' — if that count goes negative, there are too many '('. Passing both means some assignment works.

Approach

  1. 1

    openCount: +1 for '(' or '*', −1 for ')'.

  2. 2

    closeCount (from the right): +1 for ')' or '*', −1 for '('.

  3. 3

    Either negative → False.

  4. 4

    Earlier version: two stacks of indices.

நான் எப்படித் தீர்த்தேன்

Two-direction countingPythonO(n) time · O(1) space
class Solution:
    def checkValidString(self, s):
        openCount = 0
        closeCount = 0
        n = len(s)
        for i in range(n):
            if s[i] == "(" or s[i] == "*":
                openCount += 1
            else:
                openCount -= 1
            if s[n - i - 1] == ")" or s[n - i - 1] == "*":
                closeCount += 1
            else:
                closeCount -= 1
            if openCount < 0 or closeCount < 0:
                return False
        return True
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Valid Parenthesis String — அடிக்கடி கேட்கப்படும் கேள்விகள்

Valid Parenthesis String பின்னாலுள்ள intuition என்ன?

Scan left to right treating every '*' as '(' — if the open count ever goes negative, there are too many ')'. Scan right to left treating every '*' as ')' — if that count goes negative, there are too many '('. Passing both means some assignment works.

Valid Parenthesis String (LeetCode 678) ஐ எப்படித் தீர்ப்பது?

1. openCount: +1 for '(' or '*', −1 for ')'. 2. closeCount (from the right): +1 for ')' or '*', −1 for '('. 3. Either negative → False. 4. Earlier version: two stacks of indices.

Valid Parenthesis String இன் time மற்றும் space complexity என்ன?

Time: O(n). Space: O(1). (Accepted approach: Two-direction counting.)

Valid Parenthesis String இல் பொதுவான தவறுகள் என்ன?

A '*' before a '(' can't close it — the stack version checks index order.