Skip to content

678. Valid Parenthesis String #170

Discussion options

You must be logged in to vote

We can use a greedy approach with two counters to track the possible minimum and maximum number of open parentheses ( that could be valid at any point in the string.

Key Idea:

  • We maintain two counters: minOpen and maxOpen.

    • minOpen: Tracks the minimum number of open parentheses that could still be valid.
    • maxOpen: Tracks the maximum number of open parentheses that could still be valid.
  • As we iterate through the string:

    • If the current character is '(', we increment both minOpen and maxOpen since we have one more possible open parenthesis.
    • If the current character is ')', we decrement both minOpen and maxOpen since we close one open parenthesis.
    • If the current character is '*', we incr…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Sep 11, 2024
Maintainer Author

@basharul-siddike
Comment options

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants