LeetCode-in-Dart

22. Generate Parentheses

Medium

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:

Solution

class Solution {
  List<String> generateParenthesis(int n) {
    return parenthesis(n, n);
  }

  List<String> parenthesis(int left, int right) {
    if (left == 0 && right == 0) {
      return [""];
    }
    List<String> return_ls = [];
    if (left != 0) {
      List<String> l1 = parenthesis(left - 1, right);
      for (int i = 0; i < l1.length; ++i) {
        l1[i] = '(' + l1[i];
        return_ls.add(l1[i]);
      }
    }
    if (right != 0 && left < right) {
      List<String> r1 = parenthesis(left, right - 1);
      for (int i = 0; i < r1.length; ++i) {
        r1[i] = ')' + r1[i];
        return_ls.add(r1[i]);
      }
    }
    return return_ls;
  }
}