

If arr = {“go”, “pp”, “no”, “op”}, mat = {{‘g’,’p’},
{‘d’,’o’}}.
Here, the two-letter words that can be formed from the matrix “mat” are “gp”, “gd”, “go”, “pg”, “pd”, “po”, “dg”, “dp”, “do”, “og”, “op”, “od”.
So, the words that are present in the array “arr” which can be formed from the matrix “mat” are “go” and “op”.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains an integer P, denotes the length of the array.
The second line of each test case contains, P space separated strings.
The third line of each test case contains two space separated integers N, and M, denoting the number of rows and columns in a 'mat”.
The next N lines contain M characters i.e. the elements of the matrix “mat”.
For each test case, print all the possible words in the array “arr” in a sorted manner, that can be constructed by moving sequentially in the matrix “mat”.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= P <= 10
1 <= arr[i].length <= 50
1 <= N, M <= 7
All the characters of the strings in the array “arr” and matrix “mat” contain lowercase English letters only.
All the words in the array “arr” are unique.
Time limit: 1 second
The idea here is to start the search from every cell of the matrix mat and find all possible words that can be formed starting from this character using Backtracking. While doing so, we also keep a visited matrix and each time we visit a cell, we mark this cell as visited in the visited matrix, so that we do not consider the same character again and again while doing the traversal.
void possibleWordsUtil(mat, arr, visited, i, j, n, m, curr, st):
The idea is similar to the previous approach, but here instead of searching for each possible word from the starting of each cell in the matrix mat, we implement a trie data structure and insert all the elements from the array arr into the trie. Then, instead of starting the backtracking from each cell, we start the backtracking from all the children of the root of the trie those are present in the matrix mat. For example, if there are 3 children of the root in the trie, i.e. ‘a’, ‘b’ and ‘c’, then we start the backtracking only from those cells in the matrix mat, that contains the character ‘a’, ‘b’ and ‘c’ only.
void possibleWordsUtil(root, mat, visited, i, j, n, m, curr, st):
Combination Sum III
Combination Sum III
Combination Sum III
Combination Sum III
Combination Sum III
Generate All Strings
Generate All Strings
Generate All Strings
Generate All Strings
8-Queen Problem
Sequential Digits
Expression Add Operators