This approach uses a Trie to represent the list of words for efficient prefix matching. The algorithm performs depth-first search (DFS) on the board starting from each cell. Each path of exploration is checked against the Trie to determine if it forms any of the words or prefixes thereof. The benefit of using a Trie is that we can stop the search early if the current path does not match any prefix in the Trie, thereby improving the efficiency of the search process.
Time Complexity: O(M*(4*3^(L-1))), where M is the number of cells on the board, and L is the maximum length of a word.
Space Complexity: O(N*L), where N is the number of words, and L is the maximum length of a word, used by the Trie structure.
1#include <stdio.h>
2#include <stdlib.h>
3#include <stdbool.h>
4#include <string.h>
5
6#define ALPHABET_SIZE 26
7
8typedef struct TrieNode {
9 struct TrieNode* children[ALPHABET_SIZE];
10 char* word;
11} TrieNode;
12
13TrieNode* createTrieNode() {
14 TrieNode* node = (TrieNode*)malloc(sizeof(TrieNode));
15 for (int i = 0; i < ALPHABET_SIZE; i++)
16 node->children[i] = NULL;
17 node->word = NULL;
18 return node;
19}
20
21void insertWord(TrieNode* root, char* word) {
22 TrieNode* node = root;
23 while (*word) {
24 if (!node->children[*word - 'a'])
25 node->children[*word - 'a'] = createTrieNode();
26 node = node->children[*word - 'a'];
27 word++;
28 }
29 node->word = strdup(word);
30}
31
32void findWordsDFS(char** board, int i, int j, int m, int n, TrieNode* node, bool** visited, char** result, int* returnSize) {
33 if (i < 0 || i >= m || j < 0 || j >= n || visited[i][j] || !node->children[board[i][j] - 'a'])
34 return;
35
36 node = node->children[board[i][j] - 'a'];
37 if (node->word) {
38 result[*returnSize] = strdup(node->word);
39 (*returnSize)++;
40 node->word = NULL; // Avoid duplicate printing
41 }
42
43 visited[i][j] = true;
44 findWordsDFS(board, i + 1, j, m, n, node, visited, result, returnSize);
45 findWordsDFS(board, i - 1, j, m, n, node, visited, result, returnSize);
46 findWordsDFS(board, i, j + 1, m, n, node, visited, result, returnSize);
47 findWordsDFS(board, i, j - 1, m, n, node, visited, result, returnSize);
48 visited[i][j] = false;
49}
50
51char** findWords(char** board, int boardSize, int* boardColSize, char** words, int wordsSize, int* returnSize) {
52 TrieNode* root = createTrieNode();
53 for (int i = 0; i < wordsSize; i++)
54 insertWord(root, words[i]);
55
56 *returnSize = 0;
57 char** result = (char**)malloc(1000 * sizeof(char*)); // Assumption on max result size
58 bool** visited = (bool**)malloc(boardSize * sizeof(bool*));
59 for (int i = 0; i < boardSize; i++)
60 visited[i] = (bool*)calloc(*boardColSize, sizeof(bool));
61
62 for (int i = 0; i < boardSize; i++) {
63 for (int j = 0; j < *boardColSize; j++) {
64 findWordsDFS(board, i, j, boardSize, *boardColSize, root, visited, result, returnSize);
65 }
66 }
67
68 // Free visited array
69 for (int i = 0; i < boardSize; i++)
70 free(visited[i]);
71 free(visited);
72
73 return result;
74}
This C solution constructs a Trie for the list of words and uses a recursive DFS to explore each board cell. During the exploration, it checks for word matches and adds them to the result if found. The Trie structure is dynamically built using a set of Trie nodes. The DFS ensures not to visit the same cell multiple times within the same exploration path.
In this approach, we perform backtracking for each word individually. This avoids the overhead of constructing a Trie but involves repeated board scanning. A Recursive Depth First Search (DFS) is used to explore possibilities starting from each cell on the board. We maintain a visited matrix to ensure a letter is not reused in forming a particular word.
Time Complexity: O(N * M * 3^L), where N is the number of words, M is the number of cells in the board, and L is the length of the word being searched in the worst case.
Space Complexity: O(L), for the recursive call stack.
1#include <stdio.h>
2#include <string.h>
3#include <stdbool.h>
4
5bool dfs(char** board, int boardSize, int boardColSize, char* word, int index, int i, int j, bool** visited) {
6 if (index == strlen(word)) return true;
7 if (i < 0 || i >= boardSize || j < 0 || j >= boardColSize || visited[i][j] || board[i][j] != word[index])
8 return false;
9
10 visited[i][j] = true;
11 if (dfs(board, boardSize, boardColSize, word, index + 1, i - 1, j, visited) ||
12 dfs(board, boardSize, boardColSize, word, index + 1, i + 1, j, visited) ||
13 dfs(board, boardSize, boardColSize, word, index + 1, i, j - 1, visited) ||
14 dfs(board, boardSize, boardColSize, word, index + 1, i, j + 1, visited)) {
15 return true;
16 }
17 visited[i][j] = false;
18 return false;
19}
20
21bool exist(char** board, int boardSize, int* boardColSize, char* word) {
22 bool** visited = (bool**)malloc(boardSize * sizeof(bool*));
23 for (int i = 0; i < boardSize; i++) {
24 visited[i] = (bool*)calloc(*boardColSize, sizeof(bool));
25 }
26 for (int i = 0; i < boardSize; i++) {
27 for (int j = 0; j < *boardColSize; j++) {
28 if (dfs(board, boardSize, *boardColSize, word, 0, i, j, visited)) {
29 for (int k = 0; k < boardSize; k++){
30 free(visited[k]);
31 }
32 free(visited);
33 return true;
34 }
35 }
36 }
37 for (int i = 0; i < boardSize; i++){
38 free(visited[i]);
39 }
40 free(visited);
41 return false;
42}
This C solution attempts to find each word on the board individually. We use a helper function dfs
that tries to construct the word by moving to adjacent cells recursively. The solution checks for existence of a path for each word, returning as soon as one is found.