All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0022_generate_parentheses.Solution.cs Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
namespace LeetCodeNet.G0001_0100.S0022_generate_parentheses {

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
// #Backtracking #Algorithm_II_Day_11_Recursion_Backtracking #Udemy_Backtracking/Recursion
// #Big_O_Time_O(2^n)_Space_O(n) #2023_12_26_Time_81_ms_(99.57%)_Space_48.3_MB_(12.19%)

using System.Collections.Generic;
using System.Text;

public class Solution {
    public IList GenerateParenthesis(int n) {
        StringBuilder sb = new StringBuilder();
        List ans = new List();
        return Generate(sb, ans, n, n);
    }

    private IList Generate(StringBuilder sb, List str, int open, int close) {
        if (open == 0 && close == 0) {
            str.Add(sb.ToString());
            return str;
        }
        if (open > 0) {
            sb.Append('(');
            Generate(sb, str, open - 1, close);
            sb.Length--; // Equivalent to deleting the last character in StringBuilder
        }
        if (close > 0 && open < close) {
            sb.Append(')');
            Generate(sb, str, open, close - 1);
            sb.Length--; // Equivalent to deleting the last character in StringBuilder
        }
        return str;
    }
}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy