Longest Valid Parentheses
Given a string that contains the characters '(' and ')', find the length of the longest valid parentheses substring.
We'll cover the following...
Statement
Given a string that contains the characters '('
and ')'
, find the length of the longest valid parentheses substring.
Example
Let’s look at the image below:
The longest valid parentheses substring in "(()"
is "()"
, which has a length of 2.
Sample input
s = "(()"
Expected output
2
Try it yourself
#include <iostream>#include <string>using namespace std;int LongestValidParentheses(string s) {// TODO: Write - Your - Codereturn -1;}
Solution
We can solve this problem with two passes over the input string. As we traverse the string from left to right, we count the number of left parentheses (left
) and right ...
Access this course and 1400+ top-rated courses and projects.