Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Added GenerateParentheses solution (#539)
Browse files Browse the repository at this point in the history
* Added Soluion of ContainweWithMostWater problem

* Added Example

Signed-off-by: Pranjal Goyal <[email protected]>

* Added GenerateParentheses code

* Updates

Signed-off-by: Pranjal Goyal <[email protected]>
  • Loading branch information
pranjalg13 authored Oct 12, 2021
1 parent ad237b4 commit 565d94f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Programming/Python/GenerateParentheses/GenerateParentheses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
Example - 1:
Input: n = 3
Output: ["((()))","(()())","(())()","()(())","()()()"]
"""

class Solution(object):

# Storing the generated string
def generateParenthesis(self, n):
result = []
self.generateRecurParen(n,n,"",result)
return result

# Recursive call to left and right paren
def generateRecurParen(self, left,right,temp,result):
if left == 0 and right == 0:
result.append(temp)
return
if left>0:
self.generateRecurParen(left-1,right,temp+'(',result)
if right > left:
self.generateRecurParen(left, right-1, temp + ')', result)

# Making object
object1 = Solution()
n = int(input("Enter the number of pairs: "))
print(object1.generateParenthesis(n))

0 comments on commit 565d94f

Please sign in to comment.