IS ques to find maximum length paranthesis?

How should i approach this ques?

Hello @pranjalarora98please wait i am going through the question.
i will update you soon with the approach and the code.
Happy Learning!!

just give some hints

@pranjalarora98 yes please wait.

An Efficient Solution can solve this problem in O(n) time. The idea is to store indexes of previous starting brackets in a stack. The first element of stack is a special element that provides index before beginning of valid substring (base for next valid string).

  1. Create an empty stack and push -1 to it. The first element
    of stack is used to provide base for next valid string.
  2. Initialize result as 0.
  3. If the character is ‘(’ i.e. str[i] == ‘(’), push index
    ‘i’ to the stack.
  4. Else (if the character is ‘)’)
    a) Pop an item from stack (Most of the time an opening bracket)
    b) If stack is not empty, then find length of current valid
    substring by taking difference between current index and
    top of the stack. If current length is more than result,
    then update the result.
    c) If stack is empty, push current index as base for next
    valid substring.
  5. Return result.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.