diff --git a/backtracking/generate_paranthesis.rb b/backtracking/generate_paranthesis.rb index 5881aea..587a25c 100644 --- a/backtracking/generate_paranthesis.rb +++ b/backtracking/generate_paranthesis.rb @@ -15,6 +15,16 @@ # # 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. + # @param {Integer} n # @return {String[]} def generate_parenthesis(n)