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 September 2026 (Sep 1–15)Sponsored by FLXBL

Contest runs September 1 - 15. Complete challenges to climb the leaderboard!

1st & 2nd: Salesforce certification exam voucher see all prizes

Only the 15 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