Given N pairs of parentheses “()”, return a list with all the valid permutations.
Assumptions
- N > 0
Examples
- N = 1, all valid permutations are ["()"]
- N = 3, all valid permutations are ["((()))", "(()())", "(())()", "()(())", "()()()"]
思路:先有左括号,才有右括号。所以先有右括号的情况不用考虑。
如果需要N对括号,那么出现一个左括号后,则可以出现相应数量的右括号,同时最多只能出现N-1个左括号。
代码如下:具体实现过程不是很明白,需要在纸上画整个过程。
public class Solution {
public List<String> validParentheses(int n) {
List<String> result = new ArrayList<String>();
//the final string contains 2k characters
char[] cur = new char[n * 2];
helper(cur, n, n, 0, result);
return result;
}
private void helper(char[] cur, int left, int right, int index, List<String> result) {
//terminate condition:
//when we do not have any parenthese left.
if(left == 0 && right == 0) {
result.add(new String(cur));
return;
}
//when we can add a '('? whenever there is some '(' we can still use.
if(left > 0) {
cur[index] = '(';
helper(cur, left - 1, right, index + 1, result);
}
if(right > left) {
cur[index] = ')';
helper(cur, left, right - 1, index + 1, result);
}
}
}
类似题目:https://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/
Approach: To form all the sequences of balanced bracket subsequences with n pairs. So there are n opening brackets and n closing brackets.
So the subsequence will be of length 2*n. There is a simple idea, the i’th character can be ‘{‘ if and only if the count of ‘{‘ till i’th is less than n and i’th character can be ‘}’ if and only if the count of ‘{‘ is greater than the count of ‘}’ till index i. If these two cases are followed then the resulting subsequence will always be balanced.
So form the recursive function using the above two cases.
Algorithm:
- Create a recursive function that accepts a string (s), count of opening brackets (o) and count of closing brackets (c) and the value of n.
- if the value of opening bracket and closing bracket is equal to n then print the string and return.
- If the count of opening bracket is greater than count of closing bracket then call the function recursively with the following parameters String s + “}”, count of opening bracket o, count of closing bracket c + 1, and n.
- If the count of opening bracket is less than n then call the function recursively with the following parameters String s + “{“, count of opening bracket o + 1, count of closing bracket c, and n.
import java.io.*;
class GFG
{
// Function that print all combinations of
// balanced parentheses
// open store the count of opening parenthesis
// close store the count of closing parenthesis
static void _printParenthesis(char str[], int pos, int n, int open, int close)
{
if(close == n)
{
// print the possible combinations
for(int i=0;i<str.length;i++)
System.out.print(str[i]);
System.out.println();
return;
}
else
{
if(open > close) {
str[pos] = '}';
_printParenthesis(str, pos+1, n, open, close+1);
}
if(open < n) {
str[pos] = '{';
_printParenthesis(str, pos+1, n, open+1, close);
}
}
}
// Wrapper over _printParenthesis()
static void printParenthesis(char str[], int n)
{
if(n > 0)
_printParenthesis(str, 0, n, 0, 0);
return;
}
// driver program
public static void main (String[] args)
{
int n = 3;
char[] str = new char[2 * n];
printParenthesis(str, n);
}
}