Monday, August 9, 2021

Subsequence of string using recursion.

Subsequence : If abc is a string then subsequence are - a,ab,abc,ac,b,bc,c,"".So lets find out first how we can divide this problem in suproblem .think if we split first character then the string becomes "bc".then if we find out the sequence of bc it becomes-b,bc,c,"".Now lets concatenate first splitted character "a" with the result of sequence bc ,then we will get our desired output .

  public class App {

      public static void main(String[] args) {

String word = "bc";

String subSequence = getSubSequence(word);

System.out.println(subSequence);

}


private static String getSubSequence(String word) {


if (word.isEmpty()) {

return "";

}

char first = word.charAt(0);

System.out.println(first);

String rest = getSubSequence(word.substring(1));


String result = "";

for (String subSeq : rest.split(",", -1)) {

result += "," + first + subSeq;

result += "," + subSeq;

}

return result.substring(1);

}

}

No comments:

Post a Comment

Element of a good table (Ref: Database design mere mortals by Michael J. Hernandez)

  Elements of the Ideal Table: It represents a single subject, which can be an object or event that reduces the risk of potential data integ...