File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments