Recursively print all sentences that can be formed from list of word lists - GeeksforGeeks
Given a list of word lists How to print all sentences possible taking one word from a list at a time via recursion?
Given a list of word lists How to print all sentences possible taking one word from a list at a time via recursion?
void
printUtil(string arr[R][C],
int
m,
int
n, string output[R])
{
// Add current word to output array
output[m] = arr[m][n];
// If this is last word of current output sentence, then print
// the output sentence
if
(m==R-1)
{
for
(
int
i=0; i<R; i++)
cout << output[i] <<
" "
;
cout << endl;
return
;
}
// Recur for next row
for
(
int
i=0; i<C; i++)
if
(arr[m+1][i] !=
""
)
printUtil(arr, m+1, i, output);
}
// A wrapper over printUtil()
void
print(string arr[R][C])
{
// Create an array to store sentence
string output[R];
// Consider all words for first row as starting points and
// print all sentences
for
(
int
i=0; i<C; i++)
if
(arr[0][i] !=
""
)
printUtil(arr, 0, i, output);
}
private static void recursivePrint(String[][] list, String s, int level) {
if (level == list.length) {
System.out.println(s);
return;
}
for (int j = 0; j < list[level].length; j++) {
recursivePrint(list, s + " " + list[level][j], level + 1);
}
}
Read full article from Recursively print all sentences that can be formed from list of word lists - GeeksforGeeks