66. All Valid Permutations Of Parentheses I

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:  

  1. 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.
  2. if the value of opening bracket and closing bracket is equal to n then print the string and return.
  3. 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.
  4. 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);
    }
}
  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值