How do I print all possible ways if I drop card have two sidws with C ans S n ti
ID: 3676429 • Letter: H
Question
How do I print all possible ways if I drop card have two sidws with C ans S n times using recursion method ?
For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:
CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS
What I have tried:
How do I print all possible ways if I drop card have two sidws with C ans S n times?
For example of you drop it 4 times in a given session, all possible ways to drop it are as follows:
CCCC CCCS CCSC CCSS CSCC CSCS CSSC CSSS SCCC SCCS SCSC SCSS SSCC SSCS SSSC SSSS
Explanation / Answer
//A list of strings is created, where the output will be stored.
List<String> inc = new LinkedList<>();
//Calling the method to produce all possible strings of size 4
increments(inc, 4, "");
//Printing all the strings present in linked list to output
for (String s : inc)
System.out.println(s);
public void increments(List<String> items, int length, String prefix)
{
//A string is added to list only if its length is equal to the given length
if (prefix.length() == length) {
//Every string produced out of recursion is added to the list.
items.add(prefix);
return;
}
//the two recursive functions for the letter c and s to add them to the strings
increments(items, length, prefix + 'C');
increments(items, length, prefix + 'S');
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.