Given a list of word lists How to print all sentences possible taking one word from a list at a time via recursion?
Example:
Input: {{"you", "we"},
{"have", "are"},
{"sleep", "eat", "drink"}}
Output:
you have sleep
you have eat
you have drink
you are sleep
you are eat
you are drink
we have sleep
we have eat
we have drink
we are sleep
we are eat
we are drink
We strongly recommend minimizing your browser and try this yourself first.
The idea is based on a simple depth-first traversal. We start from every word of the first list as the first word of an output sentence, then recur for the remaining lists.
Below is the C++ implementation of the above idea. In the below implementation, the input list of the list is considered as a 2D array. If we take a closer look, we can observe that the code is very close to the DFS of graph.
C++
#include <iostream>
#include <string>
#define R 3
#define C 3
using namespace std;
void printUtil(string arr[R][C], int m, int n, string output[R])
{
output[m] = arr[m][n];
if (m==R-1)
{
for ( int i=0; i<R; i++)
cout << output[i] << " " ;
cout << endl;
return ;
}
for ( int i=0; i<C; i++)
if (arr[m+1][i] != "" )
printUtil(arr, m+1, i, output);
}
void print(string arr[R][C])
{
string output[R];
for ( int i=0; i<C; i++)
if (arr[0][i] != "" )
printUtil(arr, 0, i, output);
}
int main()
{
string arr[R][C] = {{ "you" , "we" },
{ "have" , "are" },
{ "sleep" , "eat" , "drink" }};
print(arr);
return 0;
}
|
Java
import java.io.*;
class GFG{
static final int R= 3 ;
static final int C = 3 ;
static void printUtil(String [][]arr,
int m, int n,
String []output)
{
output[m] = arr[m][n];
if (m == R - 1 )
{
for ( int i = 0 ; i < R; i++)
System.out.print(output[i] + " " );
System.out.println();
return ;
}
for ( int i = 0 ; i < C; i++)
if (arr[m + 1 ][i] != "" && m < C)
printUtil(arr, m + 1 , i, output);
}
static void print(String [][]arr)
{
String []output = new String[R];
for ( int i = 0 ; i < C; i++)
if (arr[ 0 ][i] != "" )
printUtil(arr, 0 , i, output);
}
public static void main(String[] args)
{
String [][]arr = {{ "you" , "we" , "" },
{ "have" , "are" , "" },
{ "sleep" , "eat" , "drink" }};
print(arr);
}
}
|
Python3
R = 3
C = 3
def printUtil(arr, m, n, output):
output[m] = arr[m][n]
if m = = R - 1 :
for i in range (R):
print (output[i],end = " " )
print ()
return
for i in range (C):
if arr[m + 1 ][i] ! = "":
printUtil(arr, m + 1 , i, output)
def printf(arr):
output = [""] * R
for i in range (C):
if arr[ 0 ][i] ! = "":
printUtil(arr, 0 , i, output)
arr = [ [ "you" , "we" ,""],
[ "have" , "are" ,""],
[ "sleep" , "eat" , "drink" ]]
printf(arr)
|
C#
using System;
class GFG{
static readonly int R= 3;
static readonly int C =3;
static void printUtil(String [,]arr,
int m, int n,
String []output)
{
output[m] = arr[m, n];
if (m == R - 1)
{
for ( int i = 0; i < R; i++)
Console.Write(output[i] + " " );
Console.WriteLine();
return ;
}
for ( int i = 0; i < C; i++)
if (arr[m + 1, i] != "" && m < C)
printUtil(arr, m + 1, i, output);
}
static void print(String [,]arr)
{
String []output = new String[R];
for ( int i = 0; i < C; i++)
if (arr[0, i] != "" )
printUtil(arr, 0, i, output);
}
public static void Main(String[] args)
{
String [,]arr = {{ "you" , "we" , "" },
{ "have" , "are" , "" },
{ "sleep" , "eat" , "drink" }};
print(arr);
}
}
|
Javascript
<script>
const R = 3
const C = 3
function printUtil(arr, m, n, output){
output[m] = arr[m][n]
if (m == R - 1){
for (let i = 0; i < R; i++)
document.write(output[i], " " )
document.write( "</br>" )
return
}
for (let i = 0; i < C; i++){
if (arr[m+1][i] != "" )
printUtil(arr, m+1, i, output)
}
}
function printf(arr){
let output = new Array(R).fill( "" )
for (let i = 0; i < C; i++){
if (arr[0][i] != "" )
printUtil(arr, 0, i, output)
}
}
let arr = [ [ "you" , "we" , "" ],
[ "have" , "are" , "" ],
[ "sleep" , "eat" , "drink" ]]
printf(arr)
</script>
|
Output
you have sleep
you have eat
you have drink
you are sleep
you are eat
you are drink
we have sleep
we have eat
we have drink
we are sleep
we are eat
we are drink
Time Complexity: O(n^m)
Where n is the number of words in the array and m is the number of rows.
Space Complexity: O(m)
Where m is the number of rows. We need an array of size m to store the output sentences.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
31 Jan, 2023
Like Article
Save Article