refactor: ReturnSubsequence
(#5408)
* refactor: ReturnSubsequence * checkstyle: fix formatting * checkstyle: fix formatting --------- Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
parent
49d1c84cb7
commit
fc5a70edc9
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<Arguments> 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"}));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user