Skip to content

Commit c53f178

Browse files
sozelfistvil02
andauthored
Implement Parentheses Generator (#5096)
* chore: add `ParenthesesGenerator` to `DIRECTORY.md` * feat: implement Parentheses Generator * ref: change `ParenthesesGenerator`s method to `static` * ref: use parametrized tests * ref: handling exception when `n < 0` * chore: update docstrings * ref: make `ParenthesesGenerator` to be a proper utility * chore(docs): add private constructor docstring * ref(tests): move bad name suggestions * style: remove reduntant comments --------- Co-authored-by: Piotr Idzik <[email protected]>
1 parent 22310de commit c53f178

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* [MazeRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java)
1616
* [MColoring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MColoring.java)
1717
* [NQueens](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/NQueens.java)
18+
* [ParenthesesGenerator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java)
1819
* [Permutation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/Permutation.java)
1920
* [PowerSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/PowerSum.java)
2021
* [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* This class generates all valid combinations of parentheses for a given number of pairs using backtracking.
8+
*/
9+
public final class ParenthesesGenerator {
10+
private ParenthesesGenerator() {
11+
}
12+
13+
/**
14+
* Generates all valid combinations of parentheses for a given number of pairs.
15+
*
16+
* @param n The number of pairs of parentheses.
17+
* @return A list of strings representing valid combinations of parentheses.
18+
* @throws IllegalArgumentException if n is less than 0.
19+
*/
20+
public static List<String> generateParentheses(final int n) {
21+
if (n < 0) {
22+
throw new IllegalArgumentException("The number of pairs of parentheses cannot be nagative");
23+
}
24+
List<String> result = new ArrayList<>();
25+
generateParenthesesHelper(result, "", 0, 0, n);
26+
return result;
27+
}
28+
29+
/**
30+
* Helper function for generating all valid combinations of parentheses recursively.
31+
*
32+
* @param result The list to store valid combinations.
33+
* @param current The current combination being formed.
34+
* @param open The number of open parentheses.
35+
* @param close The number of closed parentheses.
36+
* @param n The total number of pairs of parentheses.
37+
*/
38+
private static void generateParenthesesHelper(List<String> result, final String current, final int open, final int close, final int n) {
39+
if (current.length() == n * 2) {
40+
result.add(current);
41+
return;
42+
}
43+
if (open < n) {
44+
generateParenthesesHelper(result, current + "(", open + 1, close, n);
45+
}
46+
if (close < open) {
47+
generateParenthesesHelper(result, current + ")", open, close + 1, n);
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.thealgorithms.backtracking;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.List;
7+
import java.util.stream.Stream;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class ParenthesesGeneratorTest {
13+
@ParameterizedTest
14+
@MethodSource("regularInputStream")
15+
void regularInputTests(int input, List<String> expected) {
16+
assertEquals(expected, ParenthesesGenerator.generateParentheses(input));
17+
}
18+
19+
@ParameterizedTest
20+
@MethodSource("negativeInputStream")
21+
void throwsForNegativeInputTests(int input) {
22+
assertThrows(IllegalArgumentException.class, () -> ParenthesesGenerator.generateParentheses(input));
23+
}
24+
25+
private static Stream<Arguments> regularInputStream() {
26+
return Stream.of(Arguments.of(0, List.of("")), Arguments.of(1, List.of("()")), Arguments.of(2, List.of("(())", "()()")), Arguments.of(3, List.of("((()))", "(()())", "(())()", "()(())", "()()()")),
27+
Arguments.of(4, List.of("(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()")));
28+
}
29+
30+
private static Stream<Arguments> negativeInputStream() {
31+
return Stream.of(Arguments.of(-1), Arguments.of(-5), Arguments.of(-10));
32+
}
33+
}

0 commit comments

Comments
 (0)