Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/TechInterview/Parenthesis.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package TechInterview;

public class Parenthesis {

// Implement an algorithm to print all valid (e.g., properly opened and closed) combinations
// of n-pairs of parentheses.

public static void parenthesis(int open, int closed, String answer) {

if (open == 0 && closed == 0) {
System.out.printf("%s\n", answer);
}
if (open > 0) {
parenthesis(open - 1, closed + 1, answer + "(");
}
if (closed > 0) {
parenthesis(open, closed - 1, answer + ")");
}

}

public static void solution(int n) {

parenthesis(n, 0, "");

}

public static void main(String[] args) {

solution(3);

}
}