From c53f178308728a1a31702a7702e902f2f17192d1 Mon Sep 17 00:00:00 2001 From: SOZEL <80200848+TruongNhanNguyen@users.noreply.github.com> Date: Fri, 5 Apr 2024 23:41:27 +0700 Subject: [PATCH] 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 <65706193+vil02@users.noreply.github.com> --- DIRECTORY.md | 1 + .../backtracking/ParenthesesGenerator.java | 50 +++++++++++++++++++ .../ParenthesesGeneratorTest.java | 33 ++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java create mode 100644 src/test/java/com/thealgorithms/backtracking/ParenthesesGeneratorTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index a75b52134..36989c416 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -15,6 +15,7 @@ * [MazeRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java) * [MColoring](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/MColoring.java) * [NQueens](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/NQueens.java) + * [ParenthesesGenerator](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java) * [Permutation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/Permutation.java) * [PowerSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/PowerSum.java) * [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java) diff --git a/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java new file mode 100644 index 000000000..8bbed4106 --- /dev/null +++ b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java @@ -0,0 +1,50 @@ +package com.thealgorithms.backtracking; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class generates all valid combinations of parentheses for a given number of pairs using backtracking. + */ +public final class ParenthesesGenerator { + private ParenthesesGenerator() { + } + + /** + * Generates all valid combinations of parentheses for a given number of pairs. + * + * @param n The number of pairs of parentheses. + * @return A list of strings representing valid combinations of parentheses. + * @throws IllegalArgumentException if n is less than 0. + */ + public static List generateParentheses(final int n) { + if (n < 0) { + throw new IllegalArgumentException("The number of pairs of parentheses cannot be nagative"); + } + List result = new ArrayList<>(); + generateParenthesesHelper(result, "", 0, 0, n); + return result; + } + + /** + * Helper function for generating all valid combinations of parentheses recursively. + * + * @param result The list to store valid combinations. + * @param current The current combination being formed. + * @param open The number of open parentheses. + * @param close The number of closed parentheses. + * @param n The total number of pairs of parentheses. + */ + private static void generateParenthesesHelper(List result, final String current, final int open, final int close, final int n) { + if (current.length() == n * 2) { + result.add(current); + return; + } + if (open < n) { + generateParenthesesHelper(result, current + "(", open + 1, close, n); + } + if (close < open) { + generateParenthesesHelper(result, current + ")", open, close + 1, n); + } + } +} diff --git a/src/test/java/com/thealgorithms/backtracking/ParenthesesGeneratorTest.java b/src/test/java/com/thealgorithms/backtracking/ParenthesesGeneratorTest.java new file mode 100644 index 000000000..9da16061d --- /dev/null +++ b/src/test/java/com/thealgorithms/backtracking/ParenthesesGeneratorTest.java @@ -0,0 +1,33 @@ +package com.thealgorithms.backtracking; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.List; +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class ParenthesesGeneratorTest { + @ParameterizedTest + @MethodSource("regularInputStream") + void regularInputTests(int input, List expected) { + assertEquals(expected, ParenthesesGenerator.generateParentheses(input)); + } + + @ParameterizedTest + @MethodSource("negativeInputStream") + void throwsForNegativeInputTests(int input) { + assertThrows(IllegalArgumentException.class, () -> ParenthesesGenerator.generateParentheses(input)); + } + + private static Stream regularInputStream() { + return Stream.of(Arguments.of(0, List.of("")), Arguments.of(1, List.of("()")), Arguments.of(2, List.of("(())", "()()")), Arguments.of(3, List.of("((()))", "(()())", "(())()", "()(())", "()()()")), + Arguments.of(4, List.of("(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()", "(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"))); + } + + private static Stream negativeInputStream() { + return Stream.of(Arguments.of(-1), Arguments.of(-5), Arguments.of(-10)); + } +}