mediumStackPattern: Mixed

Validate Nested Parentheses Solution

Problem Statement

You are given a string of parentheses. Determine whether every open parenthesis has a corresponding close parenthesis.

Examples

Example 1:
Input:()
Output:true
Explanation: A single pair of parentheses is a valid message format
Example 2:
Input:(()())
Output:true
Explanation: Nested parentheses are valid as long as every '(' has a corresponding ')'
Example 3:
Input:(
Output:false
Explanation: A single '(' without a corresponding ')' is an invalid message format

Constraints

  • The input string only contains '(' and ')'
Time: O(n) Space: O(n)
Analyze constraints and compute optimal solutions step-by-step.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.