Leetcode 22. Generate Parentheses [medium] [java]

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:
Leetcode 22. Generate Parentheses [medium] [java]_第1张图片

Consideration

  1. For length n string, it is the combination of ‘(’ plus all length n-1 string and ‘)’ plus all length n-1 string.
  2. Fort the new string, we check if it is valid by using a balance. If char is ‘(’, we plus balance by 1. Otherwise, we subtract by 1 and check if the balance is negative. If it is negative, this is invalid string. After iterating through the new string, we should check if the final balance is 0.

Solution 1: brute force
Time complexity: O(n*(2^(2n))). As there are 2^(2n) combinations for a n length string. For each n length string, we need to iterate it n times to check if it is a valid string.
space complexity: O(n*(2^(2n)))

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> combinations = new ArrayList();
        generateAll(new char[2 * n], 0, combinations);
        return combinations;
    }

    private void generateAll(char[] s, int pos, List<String> combinations) {
        if(pos == s.length) {
            if(isValid(s)) {
                combinations.add(new String(s));
            } 
        } else {
            s[pos] = '(';
            generateAll(s, pos+1, combinations);
            s[pos] = ')';
            generateAll(s, pos+1, combinations);
        }  
    }
    
    private boolean isValid(char[] s) {
        int balance = 0;
        for(char c : s) {
            if(c=='(')
                balance++;
            else {
                balance--;
                if(balance < 0)
                    return false;
            }
        }
        return balance== 0;
    }
}

Solution 2: backtracking
Consideration

  1. When we add the ‘(’ or ‘)’ to the string, we guarantee it is a valid string. When add ‘(’, the append is valid if the count of open ‘(’ is less than n. When add’)’, the append is valid if the count of close ‘)’ is less than the count of open ‘)’.
    Leetcode 22. Generate Parentheses [medium] [java]_第2张图片
    I don’t understand this part!!!
class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> combinations = new ArrayList();
        backtrack(combinations,"", 0, 0, n);
        return combinations;
    }

    private void backtrack(List<String> combinations, String s, int open, int close, int max) {
        if(s.length() == 2*max)
            combinations.add(s);
        else {
            if(open < max)
                backtrack(combinations, s+"(", open+1, close, max);
            if(close < open)
                backtrack(combinations, s+")", open, close+1, max);
        }
    }    
}

Solution 3: Closure Number
在这里插入图片描述

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList();
        if (n == 0) {
            ans.add("");
        } else {
            for (int c = 0; c < n; ++c)
                for (String left: generateParenthesis(c))
                    for (String right: generateParenthesis(n-1-c))
                        ans.add("(" + left + ")" + right);
        }
        return ans;
    }
    
}

Reference
https://leetcode.com/problems/generate-parentheses/solution/

你可能感兴趣的:(leetcode)