Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Generate Parentheses

class Solution {
	List<String> generateParentheses(int n) {
	    List<String> result = new ArrayList<>();
        if (n == 0) {
			String empty = "";
            result.add(empty);
        } 
		else {
            for (int closure = 0; closure < n; closure++)
                for (String left: generateParentheses(closure))
                    for (String right: generateParentheses(n - closure - 1))
                        result.add("(" + left + ")" + right);
        }
        return result;
	}
}
Source by workat.tech #
 
PREVIOUS NEXT
Tagged: #Generate #Parentheses
ADD COMMENT
Topic
Name
3+4 =