-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGroup_Anagrams.c
More file actions
62 lines (54 loc) · 1.81 KB
/
Copy pathGroup_Anagrams.c
File metadata and controls
62 lines (54 loc) · 1.81 KB
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
/*
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using all the original letters exactly once.
Example 1:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
Example 2:
Input: strs = [""]
Output: [[""]]
Example 3:
Input: strs = ["a"]
Output: [["a"]]
* */
#define KEYLEN 128
typedef struct {
char key[KEYLEN];
int listSize;
char **list;
UT_hash_handle hh;
} Map;
int cmpFunc(const void *a, const void *b) { return *(char *) a - *(char *) b; }
char ***groupAnagrams(char **strs, int strsSize, int **columnSizes, int *returnSize) {
Map *map = NULL, *elem = NULL, *tmp;
char strBuf[KEYLEN], ***result;
int mapIndx = 0;
if (strsSize <= 0) return NULL;
for (int i = 0; i < strsSize; i++) {
strncpy(strBuf, strs[i], KEYLEN);
qsort(strBuf, strlen(strs[i]), sizeof(char), cmpFunc);
HASH_FIND_STR(map, strBuf, elem);
if (!elem) {
elem = malloc(sizeof(Map));
strncpy(elem->key, strBuf, KEYLEN);
elem->listSize = 1;
elem->list = malloc(sizeof(char **));
elem->list[0] = strs[i];
HASH_ADD_STR(map, key, elem);
} else {
elem->listSize++;
elem->list = realloc(elem->list, elem->listSize * sizeof(char **));
elem->list[elem->listSize - 1] = strs[i];
}
}
*returnSize = HASH_COUNT(map);
result = malloc(*returnSize * sizeof(char **));
*columnSizes = malloc(*returnSize * sizeof(int));
HASH_ITER(hh, map, elem, tmp)
{
(*columnSizes)[mapIndx] = elem->listSize;
result[mapIndx++] = elem->list;
}
return result;
}