Given a string containing only the characters '(' and ')', return the length of the longest valid (well-formed) parentheses substring. A valid parentheses substring has every opening parenthesis matched by a closing parenthesis in the correct order.

Examples:

Input: s = '(()' Output: 2 Explanation: The longest valid parentheses substring is '()' Input: s = '()()' Output: 4 Explanation: The entire string is a valid parentheses substring Input: s = '(())' Output: 4 Explanation: The entire string is a valid parentheses substring Input: s = ')' Output: 0 Explanation: No valid parentheses substring exists Input: s = '' Output: 0 Explanation: Empty string has no valid parentheses substring
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay August 2026

Contest runs August 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In