|
1 | 1 | package com.thealgorithms.others;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
4 |
| - |
| 3 | +/** |
| 4 | + * Class for generating all subsequences of a given string. |
| 5 | + */ |
5 | 6 | public final class ReturnSubsequence {
|
6 | 7 | private ReturnSubsequence() {
|
7 | 8 | }
|
8 | 9 |
|
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 |
| - |
22 | 10 | /**
|
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. |
25 | 15 | */
|
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 |
32 | 19 | }
|
33 |
| - String[] smallAns = returnSubsequence(givenString.substring(1)); // recursive call to get subsequences of substring starting from index |
34 |
| - // position=1 |
35 | 20 |
|
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)); |
38 | 23 |
|
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]; |
43 | 33 | }
|
44 |
| - return ans; |
| 34 | + |
| 35 | + return result; |
45 | 36 | }
|
46 | 37 | }
|
0 commit comments