Skip to content

Commit ec051ec

Browse files
Word Search
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 895c370 commit ec051ec

File tree

3 files changed

+200
-1
lines changed

3 files changed

+200
-1
lines changed

079_word_search/word_search.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ static bool dfs(char *word, char **board, bool *used,
3737
return result;
3838
}
3939

40-
static bool exist(char** board, int boardRowSize, int boardColSize, char* word) {
40+
static bool exist(char** board, int boardRowSize, int boardColSize, char* word)
41+
{
4142
int i, j;
4243
int len = strlen(word);
4344
if (len > boardRowSize * boardColSize) {

212_word_search_ii/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test word_search.c

212_word_search_ii/word_search.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <stdbool.h>
4+
#include <string.h>
5+
6+
struct trie {
7+
struct trie *subs[27];
8+
char letters[27];
9+
int num;
10+
};
11+
12+
static struct trie* trie_create()
13+
{
14+
struct trie *obj = malloc(sizeof(*obj));
15+
memset(obj->letters, 0xff, sizeof(obj->letters));
16+
memset(&obj->subs[0], 0, sizeof(obj->subs));
17+
obj->num = 0;
18+
return obj;
19+
}
20+
21+
static void trie_insert(struct trie* obj, char* word)
22+
{
23+
while (*word != '\0') {
24+
int pos = *word - 'a' + 1;
25+
obj->letters[pos] = *word++;
26+
obj->num++;
27+
if (obj->subs[pos] == NULL) {
28+
obj->subs[pos] = trie_create();
29+
}
30+
obj = obj->subs[pos];
31+
}
32+
obj->letters[0] = '\0';
33+
}
34+
35+
static void trie_remove(struct trie* obj, char* word)
36+
{
37+
while (obj != NULL && *word != '\0') {
38+
int pos = *word++ - 'a' + 1;
39+
if (--obj->num == 0) {
40+
obj->letters[pos] = 0xff;
41+
}
42+
obj = obj->subs[pos];
43+
}
44+
if (obj != NULL) {
45+
obj->letters[0] = 0xff;
46+
}
47+
}
48+
49+
static void dfs(char **board, int row_size, int col_size, int row, int col, struct trie *node,
50+
char *word, int len, bool *used, char **results, int *count, struct trie *root, int *wordsSize)
51+
{
52+
char c = board[row][col];
53+
int index = c - 'a' + 1;
54+
55+
if (node == NULL || node->letters[index] == -1) {
56+
return;
57+
}
58+
59+
printf("%d %d\n", row, col);
60+
word[len] = c;
61+
62+
struct trie *sub = node->subs[index];
63+
if (sub != NULL && sub->letters[0] == '\0') {
64+
word[len + 1] = '\0';
65+
results[*count] = malloc(len + 2);
66+
strcpy(results[*count], word);
67+
(*count)++;
68+
trie_remove(root, word);
69+
(*wordsSize)--;
70+
printf("%s\n", word);
71+
}
72+
73+
used[row * col_size + col] = true;
74+
75+
if (row > 0 && !used[(row - 1) * col_size + col]) {
76+
dfs(board, row_size, col_size, row - 1, col, node->subs[index], word, len + 1, used, results, count, root, wordsSize);
77+
}
78+
79+
if (row < row_size - 1 && !used[(row + 1) * col_size + col]) {
80+
dfs(board, row_size, col_size, row + 1, col, node->subs[index], word, len + 1, used, results, count, root, wordsSize);
81+
}
82+
83+
if (col > 0 && !used[row * col_size + col - 1]) {
84+
dfs(board, row_size, col_size, row, col - 1, node->subs[index], word, len + 1, used, results, count, root, wordsSize);
85+
}
86+
87+
if (col < col_size - 1 && !used[row * col_size + col + 1]) {
88+
dfs(board, row_size, col_size, row, col + 1, node->subs[index], word, len + 1, used, results, count, root, wordsSize);
89+
}
90+
91+
used[row * col_size + col] = false;
92+
}
93+
94+
/**
95+
* Return an array of size *returnSize.
96+
* Note: The returned array must be malloced, assume caller calls free().
97+
*/
98+
static char **findWords(char** board, int boardRowSize, int boardColSize, char** words, int wordsSize, int* returnSize)
99+
{
100+
int i, j, cap = 5000;
101+
struct trie *root = trie_create();
102+
for (i = 0; i < wordsSize; i++) {
103+
trie_insert(root, words[i]);
104+
}
105+
106+
*returnSize = 0;
107+
char *word = malloc(boardRowSize * boardColSize + 1);
108+
memset(word, 0, boardRowSize * boardColSize + 1);
109+
char **results = malloc(cap * sizeof(char *));
110+
bool *used = malloc(boardRowSize * boardColSize);
111+
memset(used, false, boardRowSize * boardColSize);
112+
for (i = 0; i < boardRowSize; i++) {
113+
for (j = 0; j < boardColSize; j++) {
114+
if (wordsSize == 0) {
115+
return results;
116+
}
117+
dfs(board, boardRowSize, boardColSize, i, j, root, word, 0, used, results, returnSize, root, &wordsSize);
118+
}
119+
}
120+
return results;
121+
}
122+
123+
int main(void)
124+
{
125+
int i;
126+
#if 0
127+
int row = 4;
128+
int col = 4;
129+
char **board = malloc(row * sizeof(char *));
130+
board[0] = malloc(col);
131+
board[0][0] = 'o';
132+
board[0][1] = 'a';
133+
board[0][2] = 'a';
134+
board[0][3] = 'n';
135+
board[1] = malloc(col);
136+
board[1][0] = 'e';
137+
board[1][1] = 't';
138+
board[1][2] = 'a';
139+
board[1][3] = 'e';
140+
board[2] = malloc(col);
141+
board[2][0] = 'i';
142+
board[2][1] = 'h';
143+
board[2][2] = 'k';
144+
board[2][3] = 'r';
145+
board[3] = malloc(col);
146+
board[3][0] = 'i';
147+
board[3][1] = 'f';
148+
board[3][2] = 'l';
149+
board[3][3] = 'v';
150+
151+
int size = 4;
152+
char **words = malloc(size * sizeof(char *));
153+
words[0] = malloc(5);
154+
strcpy(words[0], "oath");
155+
words[1] = malloc(5);
156+
strcpy(words[1], "pea");
157+
words[2] = malloc(5);
158+
strcpy(words[2], "eat");
159+
words[3] = malloc(5);
160+
strcpy(words[3], "rain");
161+
#else
162+
int row = 2;
163+
int col = 2;
164+
char **board = malloc(row * sizeof(char *));
165+
board[0] = malloc(col);
166+
board[0][0] = 'a';
167+
board[0][1] = 'b';
168+
board[1] = malloc(col);
169+
board[1][0] = 'a';
170+
board[1][1] = 'a';
171+
172+
int size = 7;
173+
char **words = malloc(size * sizeof(char *));
174+
words[0] = malloc(5);
175+
words[1] = malloc(5);
176+
words[2] = malloc(5);
177+
words[3] = malloc(5);
178+
words[4] = malloc(5);
179+
words[5] = malloc(5);
180+
words[6] = malloc(5);
181+
strcpy(words[0], "aba");
182+
strcpy(words[1], "baa");
183+
strcpy(words[2], "bab");
184+
strcpy(words[3], "aaab");
185+
strcpy(words[4], "aaa");
186+
strcpy(words[5], "aaaa");
187+
strcpy(words[6], "aaba");
188+
#endif
189+
190+
int count = 0;
191+
char **lists = findWords(board, row, col, words, size, &count);
192+
for (i = 0; i < count; i++) {
193+
printf("%s\n", lists[i]);
194+
}
195+
return 0;
196+
}

0 commit comments

Comments
 (0)