bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 32. Longest Valid Parentheses

32. Longest Valid Parentheses

Leetcode Solutions

Approach: Using Stack

  1. Initialize max_length to 0 and a stack with -1.
  2. Iterate through the string: a. If the character is (, push its index onto the stack. b. If the character is ), pop the top of the stack. c. If the stack is empty after popping, push the current index onto the stack. d. If the stack is not empty, update max_length with the maximum of max_length and the difference between the current index and the top of the stack.
  3. Return max_length.
UML Thumbnail

Approach: Without extra space

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...