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 <halfpacho@gmail.com>
This commit is contained in:
Mindaugas 2023-02-28 07:17:31 +00:00 committed by GitHub
parent 0bc8f7a576
commit 3c8f86e740
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 79 additions and 0 deletions

View File

@ -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 |

78
leetcode/src/17.c Normal file
View File

@ -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 <stdlib.h> // for the malloc() function
#include <string.h> // 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;
}