aboutsummaryrefslogtreecommitdiff
path: root/Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c
diff options
context:
space:
mode:
Diffstat (limited to 'Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c')
-rw-r--r--Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c b/Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c
new file mode 100644
index 0000000..eeabd21
--- /dev/null
+++ b/Computer_Science/leetcode/17-letter_combinations_of_a_phone_number.c
@@ -0,0 +1,46 @@
+/**
+ * Return an array of size *returnSize.
+ * Note: The returned array must be malloced, assume caller calls free().
+ */
+void helper(char **result, char *map[10], char *digits, char *tmp, int index, int *returnSize)
+{
+ char *p = map[*digits - '0'];
+ for(; *p; ++p) {
+ tmp[index] = *p;
+ helper(result, map, digits + 1, tmp, index + 1, returnSize);
+ }
+ if(*digits == '\0') {
+ *(result + *returnSize) = malloc(sizeof(char) * index + 1);
+ for(int i = 0; i < index; i++) {
+ *(*(result + *returnSize) + i) = tmp[i];
+ }
+ *(*(result + *returnSize) + index) = '\0';
+ ++*returnSize;
+ }
+}
+char** letterCombinations(char* digits, int* returnSize) {
+ int len = strlen(digits);
+ int index = 0;
+ char tmp[len + 1];
+ char **result = malloc(sizeof(int *) * 100);
+ char *map[10] = {
+ " ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
+ };
+
+ *returnSize = 0;
+
+ if(len < 1)
+ return result;
+
+ while(*digits) {
+ if(*digits >= '2' && *digits <= '9') {
+ helper(result, map, digits, tmp, index, returnSize);
+ } else {
+ *returnSize = 0;
+ break;
+ }
+ ++digits;
+ }
+
+ return result;
+}