mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-15 03:43:22 +01:00
Merge pull request #119 from TheAlgorithms/generate_parenthesis
Generate parenthesis
This commit is contained in:
commit
4fa7e1a672
2 changed files with 109 additions and 0 deletions
|
@ -1,4 +1,7 @@
|
|||
|
||||
## Backtracking
|
||||
* [Generate Paranthesis](https://github.com/TheAlgorithms/Ruby/blob/master/backtracking/generate_paranthesis.rb)
|
||||
|
||||
## Bit Manipulation
|
||||
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/bit_manipulation/power_of_two.rb)
|
||||
|
||||
|
|
106
backtracking/generate_paranthesis.rb
Normal file
106
backtracking/generate_paranthesis.rb
Normal file
|
@ -0,0 +1,106 @@
|
|||
# Given n pairs of parentheses, write a function to generate all combinations
|
||||
# of well-formed parentheses.
|
||||
#
|
||||
# Example 1:
|
||||
#
|
||||
# Input: n = 3
|
||||
# Output: ["((()))","(()())","(())()","()(())","()()()"]
|
||||
# Example 2:
|
||||
#
|
||||
# Input: n = 1
|
||||
# Output: ["()"]
|
||||
#
|
||||
#
|
||||
# Constraints:
|
||||
#
|
||||
# 1 <= n <= 8
|
||||
|
||||
# Approach:
|
||||
#
|
||||
# Let's only add '(' or ')' when we know it will remain a valid sequence.
|
||||
# We can do this by keeping track of the number of opening and closing brackets
|
||||
# we have placed so far.
|
||||
#
|
||||
# We can start an opening bracket if we still have one (of n) left to place.
|
||||
# And we could start a closing bracket if it'd not exceed the number of opening
|
||||
# brackets.
|
||||
|
||||
# Complexity Analysis
|
||||
#
|
||||
# Time Complexity: O(4^n/sqrt(n)). Each valid sequence has at most n steps during the backtracking procedure.
|
||||
# Space Complexity: O(4^n/sqrt(n)), as described above, and using O(n) space to store the sequence.
|
||||
|
||||
# Refer to the attached diagram for recursion,
|
||||
# The numbers next to each node are the counts of left and right parantheses
|
||||
|
||||
# @param {Integer} n
|
||||
# @return {String[]}
|
||||
def generate_parenthesis(n)
|
||||
parenthesis = []
|
||||
backtrack(parenthesis, "", 0, 0, n)
|
||||
parenthesis
|
||||
end
|
||||
|
||||
def backtrack(parenthesis, curr, open, close, max)
|
||||
if curr.length == max * 2
|
||||
parenthesis.push(curr)
|
||||
return
|
||||
end
|
||||
|
||||
if open < max
|
||||
backtrack(parenthesis, curr + "(", open + 1, close, max)
|
||||
end
|
||||
|
||||
if close < open
|
||||
backtrack(parenthesis, curr + ")", open, close + 1, max)
|
||||
end
|
||||
end
|
||||
|
||||
n = 3
|
||||
print(generate_parenthesis(n))
|
||||
# Output: ["((()))","(()())","(())()","()(())","()()()"]
|
||||
|
||||
# *** Example: n = 3 *** Space after each DFS instance
|
||||
# backtrack called with 0 0 []
|
||||
# backtrack called with ( 1 0 []
|
||||
# backtrack called with (( 2 0 []
|
||||
# backtrack called with ((( 3 0 []
|
||||
# backtrack called with ((() 3 1 []
|
||||
# backtrack called with ((()) 3 2 []
|
||||
# backtrack called with ((())) 3 3 []
|
||||
# backtrack return with ((()) 3 2 ["((()))"]
|
||||
# backtrack return with ((() 3 1 ["((()))"]
|
||||
# backtrack return with ((( 3 0 ["((()))"]
|
||||
# backtrack called with (() 2 1 ["((()))"]
|
||||
# backtrack called with (()( 3 1 ["((()))"]
|
||||
# backtrack called with (()() 3 2 ["((()))"]
|
||||
# backtrack called with (()()) 3 3 ["((()))"]
|
||||
# backtrack return with (()() 3 2 ["((()))", "(()())"]
|
||||
# backtrack return with (()( 3 1 ["((()))", "(()())"]
|
||||
# backtrack called with (()) 2 2 ["((()))", "(()())"]
|
||||
# backtrack called with (())( 3 2 ["((()))", "(()())"]
|
||||
# backtrack called with (())() 3 3 ["((()))", "(()())"]
|
||||
# backtrack return with (())( 3 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (()) 2 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (() 2 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with (( 2 0 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with () 1 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()( 2 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(( 3 1 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(() 3 2 ["((()))", "(()())", "(())()"]
|
||||
# backtrack called with ()(()) 3 3 ["((()))", "(()())", "(())()"]
|
||||
# backtrack return with ()(() 3 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack return with ()(( 3 1 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()() 2 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack called with ()()() 3 3 ["((()))", "(()())", "(())()", "()(())"]
|
||||
# backtrack return with ()()( 3 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ()() 2 2 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ()( 2 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with () 1 1 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with ( 1 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
# backtrack return with 0 0 ["((()))", "(()())", "(())()", "()(())", "()()()"]
|
||||
|
||||
n = 1
|
||||
print(generate_parenthesis(n))
|
||||
# Output: ["()"]
|
Loading…
Reference in a new issue