Ninja has given a list of unique words 'WORDS' of size 'N' and he wants to find all the words in the list formed after concatenating two or more words in the same list.
As Ninja's best friend, he asked you to help him with the above problem. So, your task is to find all words in the list which are formed after concatenating two or more words in the same list.
Print words in any order.
Note: One word can be concatenated multiple times. It is guaranteed that there is at least one word in the list, which is formed after concatenating two or more words.
Example:Input: 'WORDS' = ["ninjas", "coding", "codingninjas"]
Output: ["codingninjas"]
Only word "codingninjas' is formed after concatenating two or more words in the list i.e "coding" and "ninjas".
The first line of input contains an integer 'T', denoting the number of test cases.
The first line contains an integer 'N' size of the list 'WORDS' for each test case. The next line contains 'N' words
Output format :
For each test case, print all the formed words after concatenating two or more words in the input list.
Output for each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
2 <= 'N' <= 10^2
1 <= 'WORDS[i].LENGTH'<= 10^2
Time Limit: 1 sec
2
3
ninjas coding codingninjas
4
cat dog catdog catdogcat
codingninjas
catdog catdogcat
For the first test case, the Only word "codingninjas' is formed after concatenating two or more words in the list, i.e., "coding" and "ninjas".
For the second test case, the words "catdog" and "catdogcat" concatenate two or more words in the list.
2
4
line input inputline inputlineline
3
solution problem problemsolution
inputline inputlineline
problemsolution
Can you try to match all possible combinations recursively?
Approach: In this approach, for every string, we will try to match strings in the 'words' with it. We will sort out the array of strings with the size of the string, and then for every string will try to divide it into the possible substrings such that every substring contains a string in a 'words'. We will do this recursively by checking for every possible combination.
Algorithm :
‘Compare’ function compares the vectors ‘a’ and ‘b’.
compare(a, b):
‘get’ function will return true if current word is formed after concatenating some strings in input array or not else it will return false.
get(index, word, visited):
O(M * N!), Where 'N' is the size of the input vector 'words' and 'M' is the max size of 'words[i]'.
As we are recursively checking for all possible permutations and, there can be N! permutations in the worst case. And for each permutation we are iterating over, it will consume another linear time. hence the time complexity will be O(M * M!).
O(M * N), Where 'N' is the size of the input vector 'words' and 'M' is the max size of 'words[i]'.
As we are using extra 'visited' and 'answer' arrays of max size 'N' and with max size of the word in it 'M', the space complexity will be O(N * M).