2019-05-09 19:32:54 +08:00
|
|
|
package Others;
|
2017-10-02 02:01:01 +08:00
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
public class ReturnSubsequence {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
System.out.println("Enter String: ");
|
2019-05-09 19:32:54 +08:00
|
|
|
Scanner s = new Scanner(System.in);
|
|
|
|
String givenString = s.next(); //given string
|
|
|
|
String[] subsequence = returnSubsequence(givenString); //calling returnSubsequence() function
|
2017-10-02 02:01:01 +08:00
|
|
|
System.out.println("Subsequences : ");
|
2019-05-09 19:32:54 +08:00
|
|
|
//print the given array of subsequences
|
|
|
|
for (int i = 0; i < subsequence.length; i++) {
|
2017-10-02 02:01:01 +08:00
|
|
|
System.out.println(subsequence[i]);
|
|
|
|
}
|
2020-01-29 00:34:52 +08:00
|
|
|
s.close();
|
2017-10-02 02:01:01 +08:00
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param givenString
|
|
|
|
* @return subsequence
|
|
|
|
*/
|
2017-10-02 02:01:01 +08:00
|
|
|
private static String[] returnSubsequence(String givenString) {
|
2019-05-09 19:32:54 +08:00
|
|
|
if (givenString.length() == 0) // If string is empty we will create an array of size=1 and insert "" (Empty string) in it
|
2017-10-02 02:01:01 +08:00
|
|
|
{
|
2019-05-09 19:32:54 +08:00
|
|
|
String[] ans = new String[1];
|
|
|
|
ans[0] = "";
|
|
|
|
return ans;
|
2017-10-02 02:01:01 +08:00
|
|
|
|
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
String[] SmallAns = returnSubsequence(givenString.substring(1)); //recursive call to get subsequences of substring starting from index position=1
|
2017-10-02 02:01:01 +08:00
|
|
|
|
2019-05-09 19:32:54 +08:00
|
|
|
String[] ans = new String[2 * SmallAns.length];// Our answer will be an array off string of size=2*SmallAns
|
|
|
|
int i = 0;
|
|
|
|
for (; i < SmallAns.length; i++) {
|
|
|
|
ans[i] = SmallAns[i]; //Copying all the strings present in SmallAns to ans string array
|
2017-10-02 02:01:01 +08:00
|
|
|
}
|
2019-05-09 19:32:54 +08:00
|
|
|
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
|
2017-10-02 02:01:01 +08:00
|
|
|
}
|
|
|
|
return ans;
|
|
|
|
}
|
|
|
|
}
|