Below is the implementation of the parentheses validator:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/** * Find the length of the longest valid parentheses substring. * Stack-Based approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesStack(s) { const n = s.length; const stack = [-1]; // Initialize with -1 as a base for calculating lengths let maxLength = 0; for (let i = 0; i < n; i++) { if (s[i] === '(') { stack.push(i); } else { // s[i] === ')' stack.pop(); if (stack.length === 0) { stack.push(i); // New base } else { maxLength = Math.max(maxLength, i - stack[stack.length - 1]); } } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Dynamic Programming approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesDP(s) { const n = s.length; const dp = new Array(n).fill(0); let maxLength = 0; for (let i = 1; i < n; i++) { if (s[i] === ')') { if (s[i - 1] === '(') { // Case: "()" dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2; } else if (i - dp[i - 1] > 0 && s[i - dp[i - 1] - 1] === '(') { // Case: "))" where there's a matching "(" for the first ")" dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] >= 2 ? dp[i - dp[i - 1] - 2] : 0); } maxLength = Math.max(maxLength, dp[i]); } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Two-Pass approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesTwoPass(s) { const n = s.length; let left = 0, right = 0; let maxLength = 0; // First pass: left to right for (let i = 0; i < n; i++) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (right > left) { left = right = 0; } } // Reset counters left = right = 0; // Second pass: right to left for (let i = n - 1; i >= 0; i--) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (left > right) { left = right = 0; } } return maxLength;} // Test casesconsole.log(longestValidParenthesesStack("(()")); // 2console.log(longestValidParenthesesStack(")()())")); // 4console.log(longestValidParenthesesStack("")); // 0 console.log(longestValidParenthesesDP("(()")); // 2console.log(longestValidParenthesesDP(")()())")); // 4console.log(longestValidParenthesesDP("")); // 0 console.log(longestValidParenthesesTwoPass("(()")); // 2console.log(longestValidParenthesesTwoPass(")()())")); // 4console.log(longestValidParenthesesTwoPass("")); // 0
Let's break down the implementation:
Implement the parentheses validator solution in different programming languages.
Below is the implementation of the parentheses validator in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/** * Find the length of the longest valid parentheses substring. * Stack-Based approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesStack(s) { const n = s.length; const stack = [-1]; // Initialize with -1 as a base for calculating lengths let maxLength = 0; for (let i = 0; i < n; i++) { if (s[i] === '(') { stack.push(i); } else { // s[i] === ')' stack.pop(); if (stack.length === 0) { stack.push(i); // New base } else { maxLength = Math.max(maxLength, i - stack[stack.length - 1]); } } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Dynamic Programming approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesDP(s) { const n = s.length; const dp = new Array(n).fill(0); let maxLength = 0; for (let i = 1; i < n; i++) { if (s[i] === ')') { if (s[i - 1] === '(') { // Case: "()" dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2; } else if (i - dp[i - 1] > 0 && s[i - dp[i - 1] - 1] === '(') { // Case: "))" where there's a matching "(" for the first ")" dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] >= 2 ? dp[i - dp[i - 1] - 2] : 0); } maxLength = Math.max(maxLength, dp[i]); } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Two-Pass approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesTwoPass(s) { const n = s.length; let left = 0, right = 0; let maxLength = 0; // First pass: left to right for (let i = 0; i < n; i++) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (right > left) { left = right = 0; } } // Reset counters left = right = 0; // Second pass: right to left for (let i = n - 1; i >= 0; i--) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (left > right) { left = right = 0; } } return maxLength;} // Test casesconsole.log(longestValidParenthesesStack("(()")); // 2console.log(longestValidParenthesesStack(")()())")); // 4console.log(longestValidParenthesesStack("")); // 0 console.log(longestValidParenthesesDP("(()")); // 2console.log(longestValidParenthesesDP(")()())")); // 4console.log(longestValidParenthesesDP("")); // 0 console.log(longestValidParenthesesTwoPass("(()")); // 2console.log(longestValidParenthesesTwoPass(")()())")); // 4console.log(longestValidParenthesesTwoPass("")); // 0
First, understand that we need to find the length of the longest valid (well-formed) parentheses substring.
Recognize that a valid parentheses substring must have an equal number of opening and closing parentheses, and they must be properly nested.
Use a stack to keep track of the indices of unmatched parentheses, with a special base value for calculating lengths.
Build up the solution by considering one character at a time, using previously computed values to determine the current value.
Scan the string twice, once from left to right and once from right to left, using counters to track opening and closing parentheses.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the parentheses validator problem using a different approach than shown above.
Handle the case where the input string is empty (return 0).
Handle the case where there are no valid parentheses substrings (return 0).
Handle the case where parentheses are nested within each other.
Below is the implementation of the parentheses validator:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/** * Find the length of the longest valid parentheses substring. * Stack-Based approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesStack(s) { const n = s.length; const stack = [-1]; // Initialize with -1 as a base for calculating lengths let maxLength = 0; for (let i = 0; i < n; i++) { if (s[i] === '(') { stack.push(i); } else { // s[i] === ')' stack.pop(); if (stack.length === 0) { stack.push(i); // New base } else { maxLength = Math.max(maxLength, i - stack[stack.length - 1]); } } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Dynamic Programming approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesDP(s) { const n = s.length; const dp = new Array(n).fill(0); let maxLength = 0; for (let i = 1; i < n; i++) { if (s[i] === ')') { if (s[i - 1] === '(') { // Case: "()" dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2; } else if (i - dp[i - 1] > 0 && s[i - dp[i - 1] - 1] === '(') { // Case: "))" where there's a matching "(" for the first ")" dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] >= 2 ? dp[i - dp[i - 1] - 2] : 0); } maxLength = Math.max(maxLength, dp[i]); } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Two-Pass approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesTwoPass(s) { const n = s.length; let left = 0, right = 0; let maxLength = 0; // First pass: left to right for (let i = 0; i < n; i++) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (right > left) { left = right = 0; } } // Reset counters left = right = 0; // Second pass: right to left for (let i = n - 1; i >= 0; i--) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (left > right) { left = right = 0; } } return maxLength;} // Test casesconsole.log(longestValidParenthesesStack("(()")); // 2console.log(longestValidParenthesesStack(")()())")); // 4console.log(longestValidParenthesesStack("")); // 0 console.log(longestValidParenthesesDP("(()")); // 2console.log(longestValidParenthesesDP(")()())")); // 4console.log(longestValidParenthesesDP("")); // 0 console.log(longestValidParenthesesTwoPass("(()")); // 2console.log(longestValidParenthesesTwoPass(")()())")); // 4console.log(longestValidParenthesesTwoPass("")); // 0
Let's break down the implementation:
Implement the parentheses validator solution in different programming languages.
Below is the implementation of the parentheses validator in different programming languages. Select a language tab to view the corresponding code.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/** * Find the length of the longest valid parentheses substring. * Stack-Based approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesStack(s) { const n = s.length; const stack = [-1]; // Initialize with -1 as a base for calculating lengths let maxLength = 0; for (let i = 0; i < n; i++) { if (s[i] === '(') { stack.push(i); } else { // s[i] === ')' stack.pop(); if (stack.length === 0) { stack.push(i); // New base } else { maxLength = Math.max(maxLength, i - stack[stack.length - 1]); } } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Dynamic Programming approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesDP(s) { const n = s.length; const dp = new Array(n).fill(0); let maxLength = 0; for (let i = 1; i < n; i++) { if (s[i] === ')') { if (s[i - 1] === '(') { // Case: "()" dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2; } else if (i - dp[i - 1] > 0 && s[i - dp[i - 1] - 1] === '(') { // Case: "))" where there's a matching "(" for the first ")" dp[i] = dp[i - 1] + 2 + (i - dp[i - 1] >= 2 ? dp[i - dp[i - 1] - 2] : 0); } maxLength = Math.max(maxLength, dp[i]); } } return maxLength;} /** * Find the length of the longest valid parentheses substring. * Two-Pass approach. * * @param {string} s - The input string containing only '(' and ')' * @return {number} - The length of the longest valid parentheses substring */function longestValidParenthesesTwoPass(s) { const n = s.length; let left = 0, right = 0; let maxLength = 0; // First pass: left to right for (let i = 0; i < n; i++) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (right > left) { left = right = 0; } } // Reset counters left = right = 0; // Second pass: right to left for (let i = n - 1; i >= 0; i--) { if (s[i] === '(') { left++; } else { right++; } if (left === right) { maxLength = Math.max(maxLength, left + right); } else if (left > right) { left = right = 0; } } return maxLength;} // Test casesconsole.log(longestValidParenthesesStack("(()")); // 2console.log(longestValidParenthesesStack(")()())")); // 4console.log(longestValidParenthesesStack("")); // 0 console.log(longestValidParenthesesDP("(()")); // 2console.log(longestValidParenthesesDP(")()())")); // 4console.log(longestValidParenthesesDP("")); // 0 console.log(longestValidParenthesesTwoPass("(()")); // 2console.log(longestValidParenthesesTwoPass(")()())")); // 4console.log(longestValidParenthesesTwoPass("")); // 0
First, understand that we need to find the length of the longest valid (well-formed) parentheses substring.
Recognize that a valid parentheses substring must have an equal number of opening and closing parentheses, and they must be properly nested.
Use a stack to keep track of the indices of unmatched parentheses, with a special base value for calculating lengths.
Build up the solution by considering one character at a time, using previously computed values to determine the current value.
Scan the string twice, once from left to right and once from right to left, using counters to track opening and closing parentheses.
Modify the code to implement an alternative approach and test it with the same examples.
Implement a function that solves the parentheses validator problem using a different approach than shown above.
Handle the case where the input string is empty (return 0).
Handle the case where there are no valid parentheses substrings (return 0).
Handle the case where parentheses are nested within each other.