Given a string **s** and a dictionary of words **dict****,** add spaces in **s** to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given

s = "snakesandladder",

dict = ["snake", "snakes", "and", "sand", "ladder"].

A solution is ["snakes and ladder",

"snake sand ladder"].

**Input:**

The first line contains an integer **T, **denoting the number of test cases.

Every test case has 3 lines.

The first line contains an integer **N, **number of words in the dictionary.

The second line contains** N strings** denoting the words of the dictionary.

The third line contains a string** s**.

**Output:**

For each test case, print all possible strings in one line. Each string is enclosed in (). See Example.

If no string possible print "Empty" (without quotes).

**Constraints:**

1<=T<=100

1<=N<=20

1<=Length of each word in dictionary <=15

1<=Length of s<=1000

Note: Make sure the strings are sorted in your result.

**Exapmle:**

**Input:**

1

5

lr m lrm hcdar wk

hcdarlrm

**Output:**

(hcdar lr m)(hcdar lrm)

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

murli01 | 343 |

decoder_101 | 216 |

Sulagna | 189 |

kya_bolti_public | 172 |

madhursengar24 | 172 |

PiyushPandey4 | 717 |

john_wick | 681 |

ASWATHAMA | 565 |

akhyasharma01 | 547 |

jagrit_ | 507 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.