Generate Paranthesis

Recursion Implementation Problems Easy
  • Fun Fact: The concept behind well-formed parentheses is key in developing various software tools, especially in language parsing libraries and compilers
  • Programmers often deal with nested structures like functions, loops, and conditionals in coding, which are represented using parentheses, braces, etc
  • The formation or validation of these correctly structured parentheses and other symbols is of utmost importance for the correct interpretation of the code
  • Similarly, tools for checking the validity of XML or JSON also use this concept to ensure the correct pairing and nesting of tags and braces

Given an integer n.Generate all possible combinations of well-formed parentheses of length 2 x N.

Examples:

Input : n = 3

Output : [ "((()))" , "(()())" , "(())()" , "()(())" , "()()()" ]

Input : 2

Output : [ "(())" , "()()" ]

Input : 1

Constraints

  • 1 <= n <= 8

Hints

  • Start with an empty string and keep adding ( or ) recursively. If the string has 2×n characters, add it to the result.
  • Use a backtracking approach where you explore all possibilities and backtrack when the current combination is invalid.

Company Tags

Reddit Texas Instruments DoorDash Lyft Philips Healthcare PayPal Splunk Rakuten Zomato Etsy Walmart American Express Flipkart Stripe KPMG Siemens Healthineers Zoho Intel Uber Epic Systems Unity Technologies Boston Consulting Group Bungie Cerner Freshworks TCS Cognizant Accenture Infosys Capgemini Wipro