Skip to content

Commit fc5a70e

Browse files
alxkmalxkm
and
alxkm
authored
refactor: ReturnSubsequence (TheAlgorithms#5408)
* refactor: ReturnSubsequence * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm <alx@alx.com>
1 parent 49d1c84 commit fc5a70e

File tree

2 files changed

+46
-32
lines changed

2 files changed

+46
-32
lines changed
Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Scanner;
4-
3+
/**
4+
* Class for generating all subsequences of a given string.
5+
*/
56
public final class ReturnSubsequence {
67
private ReturnSubsequence() {
78
}
89

9-
public static void main(String[] args) {
10-
System.out.println("Enter String: ");
11-
Scanner s = new Scanner(System.in);
12-
String givenString = s.next(); // given string
13-
String[] subsequence = returnSubsequence(givenString); // calling returnSubsequence() function
14-
System.out.println("Subsequences : ");
15-
// print the given array of subsequences
16-
for (int i = 0; i < subsequence.length; i++) {
17-
System.out.println(subsequence[i]);
18-
}
19-
s.close();
20-
}
21-
2210
/**
23-
* @param givenString
24-
* @return subsequence
11+
* Generates all subsequences of the given string.
12+
*
13+
* @param input The input string.
14+
* @return An array of subsequences.
2515
*/
26-
private static String[] returnSubsequence(String givenString) {
27-
if (givenString.length() == 0) { // in it // If string is empty we will create an array of
28-
// size=1 and insert "" (Empty string)
29-
String[] ans = new String[1];
30-
ans[0] = "";
31-
return ans;
16+
public static String[] getSubsequences(String input) {
17+
if (input.isEmpty()) {
18+
return new String[] {""}; // Return array with an empty string if input is empty
3219
}
33-
String[] smallAns = returnSubsequence(givenString.substring(1)); // recursive call to get subsequences of substring starting from index
34-
// position=1
3520

36-
String[] ans = new String[2 * smallAns.length]; // Our answer will be an array off string of size=2*smallAns
37-
System.arraycopy(smallAns, 0, ans, 0, smallAns.length);
21+
// Recursively find subsequences of the substring (excluding the first character)
22+
String[] smallerSubsequences = getSubsequences(input.substring(1));
3823

39-
for (int k = 0; k < smallAns.length; k++) {
40-
ans[k + smallAns.length] = givenString.charAt(0) + smallAns[k]; // Insert character at index=0 of the given
41-
// substring in front of every string
42-
// in smallAns
24+
// Create an array to hold the final subsequences, double the size of smallerSubsequences
25+
String[] result = new String[2 * smallerSubsequences.length];
26+
27+
// Copy the smaller subsequences directly to the result array
28+
System.arraycopy(smallerSubsequences, 0, result, 0, smallerSubsequences.length);
29+
30+
// Prepend the first character of the input string to each of the smaller subsequences
31+
for (int i = 0; i < smallerSubsequences.length; i++) {
32+
result[i + smallerSubsequences.length] = input.charAt(0) + smallerSubsequences[i];
4333
}
44-
return ans;
34+
35+
return result;
4536
}
4637
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
class ReturnSubsequenceTest {
11+
12+
@ParameterizedTest
13+
@MethodSource("provideTestCases")
14+
void testSubsequences(String input, String[] expected) {
15+
String[] actual = ReturnSubsequence.getSubsequences(input);
16+
assertArrayEquals(expected, actual);
17+
}
18+
19+
static Stream<Arguments> provideTestCases() {
20+
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"}),
21+
Arguments.of("aab", new String[] {"", "b", "a", "ab", "a", "ab", "aa", "aab"}));
22+
}
23+
}

0 commit comments

Comments
 (0)