Valid Permutation of Parenthesis
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A well-formed permutation of parentheses, often referred to as a "valid permutation," is a sequence that is correctly balanced and properly nested. Valid parentheses are a common feature in various computational domains, including mathematical expressions, programming languages, and data structures like trees.
Problem Definition
The problem of determining whether a sequence of parentheses is valid can be stated as follows: Given a string consisting only of the characters `(` and `)`, determine if it is a valid permutation. A valid sequence must satisfy the following conditions:
- The number of opening parentheses `(` must equal the number of closing parentheses `)`.
- At any point in the string, the number of closing parentheses should not exceed the number of opening parentheses.
Examples
Example 1
Input: `(()())`
Output: Valid
Example 2
Input: `())(`
Output: Invalid
Explanation: The third closing parenthesis does not match an opening one.
Example 3
Input: `((()))`
Output: Valid
Algorithm
A common approach to solving this problem is using a stack data structure. The stack helps efficiently manage the opening parentheses that need matching.
Step-by-Step Solution
- Initialize an empty stack.
- Iterate through each character in the string:
- If the character is an `(`, push it onto the stack.
- If it is a `)`, check if the stack is empty:
- If the stack is not empty, pop an element from the stack.
- If the stack is empty, the sequence is invalid.
- After processing all characters, if the stack is empty, the sequence is valid. Otherwise, it is invalid.
Complexity
- Time Complexity: , where is the length of the string, because each character is processed once.
- Space Complexity: , as in the worst case, all characters could be opening parentheses.
Code Example
- Compilers: Ensuring that parentheses in expressions are balanced.
- Data Parsing: Validating structured text formats like JSON and XML.
- Mathematical Expressions: Ensuring correctness in complex expressions involving nested functions or operations.
Related reading
- Value of k in k nearest neighbor algorithm
- Variadic nested loops
- Vectorizable implementation of complementary error function erfcf
- Vectorizing a gradient descent algorithm
- VC Dimension of Circle, a special case
- Venn diagram generation software from RCC8 specification or similar
- Venn Diagram Drawing Algorithms
- Verify if a list or a sublist of that list of decimal values can equal a certain sum

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.