From 3c8f86e740a0ef136f7acae713afabc11e47c352 Mon Sep 17 00:00:00 2001 From: Mindaugas <76015221+mindaugl@users.noreply.github.com> Date: Tue, 28 Feb 2023 07:17:31 +0000 Subject: [PATCH] feat: add Letter combinations of phone book problem (#1221) * feat: add Letter combinations of phone book problem (#17) * fix: add newline at the end of the file * fix: add brief description of the algorithm --------- Co-authored-by: David Leal --- leetcode/DIRECTORY.md | 1 + leetcode/src/17.c | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 leetcode/src/17.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 61afbda4..4c124798 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -20,6 +20,7 @@ | 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer) | [C](./src/13.c) | Easy | | 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix) | [C](./src/14.c) | Easy | | 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest) | [C](./src/16.c) | Medium | +| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number) | [C](./src/17.c) | Medium | | 20 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses) | [C](./src/20.c) | Easy | | 21 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists) | [C](./src/21.c) | Easy | | 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs) | [C](./src/24.c) | Medium | diff --git a/leetcode/src/17.c b/leetcode/src/17.c new file mode 100644 index 00000000..ff6e77d0 --- /dev/null +++ b/leetcode/src/17.c @@ -0,0 +1,78 @@ +/** + * Letter Combinations of a Phone Number problem + * The algorithm determines the final size of the return array (combs) and allocates + * corresponding letter for each element, assuming that the return array is alphabetically sorted. + * It does so by running two loops for each letter: + * - the first loop determines the starting positions of the sequence of subsequent letter positions + * - the second loop determines the length of each subsequent sequence for each letter + * The size and space complexity are both O("size of final array"), as even though there are 4 loops, + * each element in the final array is accessed only once. + */ + +#include // for the malloc() function +#include // for the strlen() function + +char *get_letters(char digit) { + switch (digit) { + case '2': + return "abc"; + case '3': + return "def"; + case '4': + return "ghi"; + case '5': + return "jkl"; + case '6': + return "mno"; + case '7': + return "pqrs"; + case '8': + return "tuv"; + case '9': + return "wxyz"; + default: + return ""; + } +} + +char **letterCombinations(char *digits, int *return_size) { + char *cp; + int i, j, k, l, ind, k_tot, l_tot, digits_size = 0; + + if (*digits == '\0') { + *return_size = 0; + return NULL; + } + + *return_size = 1; + cp = digits; + while (*cp != '\0') { + *return_size *= strlen(get_letters(*cp)); + digits_size++; + cp++; + } + + char **combs = malloc(sizeof(char*) * (*return_size)); + for (i = 0; i < *return_size; i++) { + combs[i] = malloc(sizeof(char) * (digits_size + 1)); + combs[i][digits_size] = '\0'; + } + + k_tot = 1; + l_tot = (*return_size); + for (i = 0; i < digits_size; i++) { // loop accross digits + cp = get_letters(digits[i]); + l_tot /= strlen(cp); + for (j = 0; j < strlen(cp); j++) { // loop accross letters of the digit + for (k = 0; k < k_tot; k++) { // loop across the subset starting positions for each letter + for (l = 0; l < l_tot; l++) { // loop accross each subset positions for each letter + ind = k * l_tot * strlen(cp) + l + l_tot * j; + combs[ind][i] = cp[j]; + } + } + } + k_tot *= strlen(cp); + } + + return combs; +}