From fc5a70edc94bda6aff4a9f6ff8f32271b62b6c3e Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Tue, 27 Aug 2024 13:12:49 +0200 Subject: [PATCH] refactor: `ReturnSubsequence` (#5408) * refactor: ReturnSubsequence * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm --- .../others/ReturnSubsequence.java | 55 ++++++++----------- .../others/ReturnSubsequenceTest.java | 23 ++++++++ 2 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 src/test/java/com/thealgorithms/others/ReturnSubsequenceTest.java diff --git a/src/main/java/com/thealgorithms/others/ReturnSubsequence.java b/src/main/java/com/thealgorithms/others/ReturnSubsequence.java index ef376c47..7ef660ce 100644 --- a/src/main/java/com/thealgorithms/others/ReturnSubsequence.java +++ b/src/main/java/com/thealgorithms/others/ReturnSubsequence.java @@ -1,46 +1,37 @@ package com.thealgorithms.others; -import java.util.Scanner; - +/** + * Class for generating all subsequences of a given string. + */ public final class ReturnSubsequence { private ReturnSubsequence() { } - public static void main(String[] args) { - System.out.println("Enter String: "); - Scanner s = new Scanner(System.in); - String givenString = s.next(); // given string - String[] subsequence = returnSubsequence(givenString); // calling returnSubsequence() function - System.out.println("Subsequences : "); - // print the given array of subsequences - for (int i = 0; i < subsequence.length; i++) { - System.out.println(subsequence[i]); - } - s.close(); - } - /** - * @param givenString - * @return subsequence + * Generates all subsequences of the given string. + * + * @param input The input string. + * @return An array of subsequences. */ - private static String[] returnSubsequence(String givenString) { - if (givenString.length() == 0) { // in it // If string is empty we will create an array of - // size=1 and insert "" (Empty string) - String[] ans = new String[1]; - ans[0] = ""; - return ans; + public static String[] getSubsequences(String input) { + if (input.isEmpty()) { + return new String[] {""}; // Return array with an empty string if input is empty } - String[] smallAns = returnSubsequence(givenString.substring(1)); // recursive call to get subsequences of substring starting from index - // position=1 - String[] ans = new String[2 * smallAns.length]; // Our answer will be an array off string of size=2*smallAns - System.arraycopy(smallAns, 0, ans, 0, smallAns.length); + // Recursively find subsequences of the substring (excluding the first character) + String[] smallerSubsequences = getSubsequences(input.substring(1)); - for (int k = 0; k < smallAns.length; k++) { - ans[k + smallAns.length] = givenString.charAt(0) + smallAns[k]; // Insert character at index=0 of the given - // substring in front of every string - // in smallAns + // Create an array to hold the final subsequences, double the size of smallerSubsequences + String[] result = new String[2 * smallerSubsequences.length]; + + // Copy the smaller subsequences directly to the result array + System.arraycopy(smallerSubsequences, 0, result, 0, smallerSubsequences.length); + + // Prepend the first character of the input string to each of the smaller subsequences + for (int i = 0; i < smallerSubsequences.length; i++) { + result[i + smallerSubsequences.length] = input.charAt(0) + smallerSubsequences[i]; } - return ans; + + return result; } } diff --git a/src/test/java/com/thealgorithms/others/ReturnSubsequenceTest.java b/src/test/java/com/thealgorithms/others/ReturnSubsequenceTest.java new file mode 100644 index 00000000..0ae30c48 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/ReturnSubsequenceTest.java @@ -0,0 +1,23 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +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; + +class ReturnSubsequenceTest { + + @ParameterizedTest + @MethodSource("provideTestCases") + void testSubsequences(String input, String[] expected) { + String[] actual = ReturnSubsequence.getSubsequences(input); + assertArrayEquals(expected, actual); + } + + static Stream provideTestCases() { + return Stream.of(Arguments.of("", new String[] {""}), Arguments.of("a", new String[] {"", "a"}), Arguments.of("ab", new String[] {"", "b", "a", "ab"}), Arguments.of("abc", new String[] {"", "c", "b", "bc", "a", "ac", "ab", "abc"}), + Arguments.of("aab", new String[] {"", "b", "a", "ab", "a", "ab", "aa", "aab"})); + } +}