Answers for "generate Pranthesis"

0

generate Pranthesis

public List<String> generateParenthesis(int n) {
	ArrayList<String> result = new ArrayList<String>();
	ArrayList<Integer> diff = new ArrayList<Integer>();
 
	result.add("");
	diff.add(0);
 
	for (int i = 0; i < 2 * n; i++) {
		ArrayList<String> temp1 = new ArrayList<String>();
		ArrayList<Integer> temp2 = new ArrayList<Integer>();
 
		for (int j = 0; j < result.size(); j++) {
			String s = result.get(j);
			int k = diff.get(j);
 
			if (i < 2 * n - 1) {
				temp1.add(s + "(");
				temp2.add(k + 1);
			}
 
			if (k > 0 && i < 2 * n - 1 || k == 1 && i == 2 * n - 1) {
				temp1.add(s + ")");
				temp2.add(k - 1);
			}
		}
 
		result = new ArrayList<String>(temp1);
		diff = new ArrayList<Integer>(temp2);
	}
 
	return result;
}
Posted by: Guest on June-23-2021
0

generate Pranthesis

bool next_balanced_sequence(string & s) {
    int n = s.size();
    int depth = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == '(')
            depth--;
        else
            depth++;

        if (s[i] == '(' && depth > 0) {
            depth--;
            int open = (n - i - 1 - depth) / 2;
            int close = n - i - 1 - open;
            string next = s.substr(0, i) + ')' + string(open, '(') + string(close, ')');
            s.swap(next);
            return true;
        }
    }
    return false;
}
Posted by: Guest on June-23-2021

Browse Popular Code Answers by Language