-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.c
50 lines (36 loc) · 1.37 KB
/
17.c
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
/* 17. Letter Combinations of a Phone Number */
/* The fun() is to merge the array of strings (a) and the char array (b).
* Then it returns the pointer to pointer to the array of new strings.
*/
char** fun(char** a, char *b, int *size) {
int len_b = strlen(b);
int len = len_b * (*size); // size of new array of strings
int w = 0;
char** res = malloc(len * sizeof(char*));
char temp[32];
for (int i = 0; i < len; i++) {
res[i] = calloc(strlen(a[0]) + 2, sizeof(char));
}
for (int i = 0; i < len_b; i++) {
for (int k = 0; k < *size; k++) {
sprintf(temp, "%s%c", a[k], b[i]);
strcpy(res[w++], temp);
}
}
*size = len;
return res;
}
char** letterCombinations(char* digits, int* returnSize) {
if (strlen(digits) < 1) return 0;
const char key[8][5] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
int len_digits = strlen(digits);
char** ret = malloc(sizeof(char*));
ret[0] = calloc(1, sizeof(char));
*returnSize = 1;
for (int i = 0; i < len_digits; i++) {
ret = fun(ret, key[digits[i] - '0' - 2], returnSize);
// ret is keep on pointing to a new array of strings. Then, use the new
// pointer to merge new characters by giving the ret as a parameter of fun();
}
return ret;
}