
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding the longest valid parentheses JavaScript
Given a string containing just the characters '(' and ')', we find the length of the longest valid (well-formed) parentheses substring.
A set of parentheses qualifies to be a well-formed parentheses, if and only if, for each opening parentheses, it contains a closing parentheses.
For example −
'(())()' is a well-formed parentheses '())' is not a well-formed parentheses '()()()' is a well-formed parentheses
Example
const str = '(())()((('; const longestValidParentheses = (str = '') => { var ts = str.split(''); var stack = [], max = 0; ts.forEach((el, ind) => { if (el == '(') { stack.push(ind); } else { if (stack.length === 0 || ts[stack[stack.length - 1]] == ')'){ stack.push(ind); } else { stack.pop(); }; } }); stack.push(ts.length); stack.splice(0, 0, -1); for (let ind = 0; ind< stack.length - 1; ind++) { let v = stack[ind+1] - stack[ind] - 1; max = Math.max(max, v); }; return max; }; console.log(longestValidParentheses(str));
Output
And the output in the console will be −
6
- Related Articles
- Longest Valid Parentheses in Python
- Valid Parentheses in C++
- Finding the longest "uncommon" sequence in JavaScript
- Finding all valid word squares in JavaScript
- Finding longest consecutive joins in JavaScript
- Finding the longest string in an array in JavaScript
- Finding the longest substring uncommon in array in JavaScript
- Finding the longest word in a string in JavaScript
- Finding the longest non-negative sum sequence using JavaScript
- Minimum Remove to Make Valid Parentheses in C++
- Minimum Add to Make Parentheses Valid in Python
- Finding longest substring between two same characters JavaScript
- Finding all the longest strings from an array in JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Maximum Nesting Depth of Two Valid Parentheses Strings in Python

Advertisements