1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
// 966. Vowel Spellchecker
#include "leetcode.h"
/*
* given a 'wordlist', we want to implement a spellchecker that converts a query
* word into a correct word. for a given 'query', the spell checker handles two
* categories of spelling mistakes: capitalisation and vowel errors. given some
* 'queries', return a list of words 'answer' where 'answer[i]' is the correct
* word for 'query = queries[i]'
*/
unsigned int rs_hash1(char *s) {
unsigned int a = 63689, b = 378551, hash = 0;
while (*s) {
hash = hash * a + (*s++);
a *= b;
}
return hash & 0x7FFFFFFF;
}
unsigned int rs_hash2(char *s) {
unsigned int a = 63689, b = 378551, hash = 0;
while (*s) {
char curr = *s;
if (curr >= 'A' && curr <= 'Z')
curr = curr - 'A' + 'a';
hash = hash * a + curr;
(*s)++;
a *= b;
}
return hash & 0x7FFFFFFF;
}
unsigned int rs_hash3(char *s) {
unsigned int a = 63689, b = 378551, hash = 0;
while (*s) {
char curr = *s;
if (curr == 'a' || curr == 'e' || curr == 'i' || curr == 'o' || curr == 'u')
curr = 'A';
else if (curr == 'A' || curr == 'E' || curr == 'I' || curr == 'O' ||
curr == 'U')
curr = 'A';
else if (curr >= 'A' && curr <= 'Z')
curr = curr - 'A' + 'a';
hash = hash * a + curr;
(*s)++;
a *= b;
}
return hash & 0x7FFFFFFF;
}
char **spellchecker(char **wordlist, int wordlistSize, char **queries,
int queriesSize, int *returnSize) {
int n = wordlistSize * 10;
unsigned int **hash1 = (unsigned int **)calloc(n, sizeof(unsigned int *));
unsigned int **hash2 = (unsigned int **)calloc(n, sizeof(unsigned int *));
unsigned int **hash3 = (unsigned int **)calloc(n, sizeof(unsigned int *));
for (int i = 0; i < wordlistSize; i++) {
unsigned int val = rs_hash1(wordlist[i]), d = val;
while (1) {
if (!hash1[d % n]) {
hash1[d % n] = (unsigned int *)malloc(2 * sizeof(unsigned int));
hash1[d % n][0] = val;
hash1[d % n][1] = i;
break;
} else
d++;
}
val = rs_hash2(wordlist[i]);
d = val;
while (1) {
if (!hash2[d % n]) {
hash2[d % n] = (unsigned int *)malloc(2 * sizeof(unsigned int));
hash2[d % n][0] = val;
hash2[d % n][1] = i;
break;
} else
d++;
}
val = rs_hash3(wordlist[i]);
d = val;
while (1) {
if (!hash3[d % n]) {
hash3[d % n] = (unsigned int *)malloc(2 * sizeof(unsigned int));
hash3[d % n][0] = val;
hash3[d % n][1] = i;
break;
} else
d++;
}
}
char **ans = (char **)malloc(queriesSize * sizeof(char *));
unsigned int val, d;
for (int i = 0; i < queriesSize; i++) {
ans[i] = (char *)malloc(8 * sizeof(char));
val = rs_hash1(queries[i]);
d = val;
while (1) {
if (!hash1[d % n])
break;
else if (hash1[d % n][0] == val) {
int pos = hash1[d % n][1];
strcpy(ans[i], wordlist[pos]);
goto next;
} else
d++;
}
val = rs_hash2(queries[i]);
d = val;
while (1) {
if (!hash2[d % n])
break;
else if (hash2[d % n][0] == val) {
int pos = hash2[d % n][1];
strcpy(ans[i], wordlist[pos]);
goto next;
} else
d++;
}
val = rs_hash3(queries[i]);
d = val;
while (1) {
if (!hash3[d % n])
break;
else if (hash3[d % n][0] == val) {
int pos = hash3[d % n][1];
strcpy(ans[i], wordlist[pos]);
goto next;
} else
d++;
}
ans[i] = "";
next:
continue;
}
free(hash1);
free(hash2);
free(hash3);
*returnSize = queriesSize;
return ans;
}
int main() {
char *w1[] = {"KiTe", "kite", "hare", "Hare"},
*q1[] = {"kite", "Kite", "KiTe", "Hare", "HARE",
"Hear", "hear", "keti", "keet", "keto"};
char *w2[] = {"yellow"}, *q2[] = {"YellOw"};
int rs1, rs2;
char **sc1 = spellchecker((char **)w1, ARRAY_SIZE(w1), (char **)q1,
ARRAY_SIZE(q1), &rs1);
char **sc2 = spellchecker((char **)w2, ARRAY_SIZE(w2), (char **)q2,
ARRAY_SIZE(q2), &rs2);
for (int i = 0; i < rs1; i++)
printf(
"%s ",
sc1[i]); // expect:
// ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
printf("\n");
for (int i = 0; i < rs2; i++)
printf("%s ", sc2[i]); // expect: ["yellow"]
printf("\n");
for (int i = 0; i < rs1; i++)
free(sc1[i]);
free(sc1);
for (int i = 0; i < rs2; i++)
free(sc2[i]);
free(sc2);
}
|