Skip to content

Commit 94e284c

Browse files
authored
Merge pull request #2844 from mdmzfzl/main
Create: 0763-partition-labels.c
2 parents 9218619 + 8b9e228 commit 94e284c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: c/0763-partition-labels.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int* partitionLabels(char* s, int* returnSize) {
2+
int* result = NULL;
3+
int* lastOccurrence = (int*)calloc(26, sizeof(int)); // Store the last occurrence index of each character
4+
5+
// Find the last occurrence index of each character
6+
for (int i = 0; s[i] != '\0'; i++) {
7+
lastOccurrence[s[i] - 'a'] = i;
8+
}
9+
10+
int start = 0, end = 0; // Pointers to track the current partition
11+
*returnSize = 0;
12+
13+
for (int i = 0; s[i] != '\0'; i++) {
14+
end = (end > lastOccurrence[s[i] - 'a']) ? end : lastOccurrence[s[i] - 'a'];
15+
16+
if (i == end) {
17+
(*returnSize)++;
18+
result = (int*)realloc(result, sizeof(int) * (*returnSize));
19+
result[(*returnSize) - 1] = end - start + 1;
20+
start = end + 1;
21+
}
22+
}
23+
24+
free(lastOccurrence);
25+
return result;
26+
}

0 commit comments

Comments
 (0)