N Pairs Parentheses

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, the problem of generating all valid combinations of n-pairs of parentheses is a classic problem. It is often used as an example problem in computer science courses and coding interviews.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268869{{"`N Pairs Parentheses`"}} python/conditional_statements -.-> lab-268869{{"`N Pairs Parentheses`"}} python/lists -.-> lab-268869{{"`N Pairs Parentheses`"}} python/tuples -.-> lab-268869{{"`N Pairs Parentheses`"}} python/function_definition -.-> lab-268869{{"`N Pairs Parentheses`"}} python/classes_objects -.-> lab-268869{{"`N Pairs Parentheses`"}} python/encapsulation -.-> lab-268869{{"`N Pairs Parentheses`"}} python/raising_exceptions -.-> lab-268869{{"`N Pairs Parentheses`"}} end

N Pairs Parentheses

Problem

The problem is to find all valid combinations of n-pairs of parentheses. A valid combination is one in which each opening parenthesis has a corresponding closing parenthesis and the pairs of parentheses are properly nested. For example, the following are valid combinations of 3-pairs of parentheses:

  • ((()))
  • (()())
  • (())()
  • ()(())
  • ()()()

The following are not valid combinations of 3-pairs of parentheses:

  • )()(
  • ((()
  • ))((
  • ()()()

Requirements

To solve this problem, we need to answer the following questions:

  • Is the input an integer representing the number of pairs?
    • Yes, the input is an integer representing the number of pairs.
  • Can we assume the inputs are valid?
    • No, we cannot assume the inputs are valid.
  • Is the output a list of valid combinations?
    • Yes, the output is a list of valid combinations.
  • Should the output have duplicates?
    • No, the output should not have duplicates.
  • Can we assume this fits memory?
    • Yes, we can assume this fits memory.

Example Usage

The following are example usages of the function:

  • None -> Exception
  • Negative -> Exception
  • 0 -> []
  • 1 -> ['()']
  • 2 -> ['(())', '()()']
  • 3 -> ['((()))', '(()())', '(())()', '()(())', '()()()']

Summary

In summary, the problem of generating all valid combinations of n-pairs of parentheses is a classic problem in computer science. To solve this problem, we need to find all valid combinations of n-pairs of parentheses, where each opening parenthesis has a corresponding closing parenthesis and the pairs of parentheses are properly nested. The output should be a list of valid combinations without duplicates.

Other Algorithm Tutorials you may like