-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path358. Rearrange String k Distance Apart.c
99 lines (72 loc) · 1.82 KB
/
358. Rearrange String k Distance Apart.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
358. Rearrange String k Distance Apart
Given a non-empty string s and an integer k, rearrange the string such that the same characters are at least distance k from each other.
All input strings are given in lowercase letters. If it is not possible to rearrange the string, return an empty string "".
Example 1:
s = "aabbcc", k = 3
Result: "abcabc"
The same letters are at least distance 3 from each other.
Example 2:
s = "aaabc", k = 3
Answer: ""
It is not possible to rearrange the string.
Example 3:
s = "aaadbbcc", k = 2
Answer: "abacabcd"
Another possible answer is: "abcabcda"
The same letters are at least distance 2 from each other.
Credits:Special thanks to @elmirap for adding this problem and creating all test cases.
*/
typedef struct {
char c;
int n;
} e_t;
int ecmp(const void *a, const void *b) {
return ((e_t *)b)->n - ((e_t *)a)->n;
}
char* rearrangeString(char* str, int k) {
e_t e[26] = { 0 };
int len = 0, i, j = 0, n;
char *s = str, *p;
if (!k) return str;
while (*s) {
i = *s - 'a';
e[i].c = *s;
e[i].n ++;
len ++;
s ++;
}
p = malloc(len + 1);
//assert(p);
p[len] = 0;
arrange:
qsort(e, 26, sizeof(e_t), ecmp);
n = 0;
for (i = 0; i < 26 && e[i].n > 0 && n < k; i ++) {
p[j ++] = e[i].c;
e[i].n --;
len --;
n ++;
}
if (len) {
if (n < k) {
p[0] = 0;
} else {
goto arrange;
}
}
return p;
}
/*
Difficulty:Hard
Total Accepted:10.3K
Total Submissions:32.3K
Companies Google
Related Topics Hash Table Heap Greedy
Similar Questions
Task Scheduler
*/