Problem Statement:
You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.
Return a list of integers representing the size of these parts.
Example 1:
Input: s = “ababcbacadefegdehijhklij”
Output: [9,7,8]
Explanation: The partition is “ababcbaca”, “defegde”, “hijhklij”. This is a partition so that each letter appears in at most one part. A partition like “ababcbacadefegde”, “hijhklij” is incorrect, because it splits s into less parts.
Example 2:
Input: s = “eccbbbbdec”
Output: [10]
Constraints
1 <= intervals.length <= 104intervals[i].length == 20 <= starti <= endi<= 105
Approach:
- For
each character, record its last occurrence in the string. Traverse the stringwhile keeping track of the current partition end (max last index seen so far).- Whenever the current index equals this partition end, we close the partition and record its size.
Time & Space Complexity:
Time Complexity: O(n)
Space Complexity: O(1)
Dry Run
Input: s = "ababcbacadefegdehijhklij"
Step 0: Start Function: partitionLabels(s) s = "ababcbacadefegdehijhklij" ans = [] first = Array(26).fill(-1) last = Array(26).fill(-1) ------------------------------------------------------------ Step 1: Fill first[] and last[] arrays 'a' → first[a]=0, last[a]=8 'b' → first[b]=1, last[b]=5 'c' → first[c]=4, last[c]=7 'd' → first[d]=9, last[d]=14 'e' → first[e]=10, last[e]=15 'f' → first[f]=11, last[f]=11 'g' → first[g]=13, last[g]=13 'h' → first[h]=16, last[h]=19 'i' → first[i]=17, last[i]=22 'j' → first[j]=18, last[j]=23 'k' → first[k]=20, last[k]=20 'l' → first[l]=21, last[l]=21 (others remain -1) ------------------------------------------------------------ Step 2: Partition calculation Initialize partitionStart = 0, partitionEnd = 0 Iteration (i = 0, s[i]='a'): partitionEnd = max(0, 8) = 8 Iteration (i = 1, s[i]='b'): partitionEnd = max(8, 5) = 8 Iteration (i = 2, s[i]='a'): partitionEnd = 8 Iteration (i = 3, s[i]='b'): partitionEnd = 8 Iteration (i = 4, s[i]='c'): partitionEnd = max(8, 7) = 8 Iteration (i = 5, s[i]='b'): partitionEnd = 8 Iteration (i = 6, s[i]='a'): partitionEnd = 8 Iteration (i = 7, s[i]='c'): partitionEnd = 8 Iteration (i = 8, s[i]='a'): i == partitionEnd (8) → Push size = 9 ans = [9] Reset partitionStart=9, partitionEnd=9 ------------------------------------------------------------ Iteration (i = 9, s[i]='d'): partitionEnd = max(9, 14) = 14 Iteration (i = 10, s[i]='e'): partitionEnd = max(14, 15) = 15 Iteration (i = 11, s[i]='f'): partitionEnd = 15 Iteration (i = 12, s[i]='e'): partitionEnd = 15 Iteration (i = 13, s[i]='g'): partitionEnd = 15 Iteration (i = 14, s[i]='d'): partitionEnd = 15 Iteration (i = 15, s[i]='e'): i == partitionEnd (15) → Push size = 7 ans = [9, 7] Reset partitionStart=16, partitionEnd=16 ------------------------------------------------------------ Iteration (i = 16, s[i]='h'): partitionEnd = max(16, 19) = 19 Iteration (i = 17, s[i]='i'): partitionEnd = max(19, 22) = 22 Iteration (i = 18, s[i]='j'): partitionEnd = max(22, 23) = 23 Iteration (i = 19, s[i]='h'): partitionEnd = 23 Iteration (i = 20, s[i]='k'): partitionEnd = 23 Iteration (i = 21, s[i]='l'): partitionEnd = 23 Iteration (i = 22, s[i]='i'): partitionEnd = 23 Iteration (i = 23, s[i]='j'): i == partitionEnd (23) → Push size = 8 ans = [9, 7, 8] ------------------------------------------------------------ Step 3: End Final ans = [9, 7, 8]
Output: [9, 7, 8]
var partitionLabels = function(s) {
let last = {};
let ans = [];
for (let i = 0; i < s.length; i++) {
last[s[i]] = i;
}
let start = 0, end = 0;
for (let i = 0; i < s.length; i++) {
end = Math.max(end, last[s[i]]);
if (i === end) {
ans.push(end - start + 1);
start = i + 1;
}
}
return ans;
};
def partition_labels(s: str):
last = [-1] * 26
for i, ch in enumerate(s):
last[ord(ch) - ord('a')] = i
ans = []
start = end = 0
for i, ch in enumerate(s):
end = max(end, last[ord(ch) - ord('a')])
if i == end:
ans.append(end - start + 1)
start = i + 1
return ans
if __name__ == "__main__":
s = input().strip()
print(" ".join(map(str, partition_labels(s))))
import java.io.*;
import java.util.*;
public class Solution {
public static List partitionLabels(String s) {
int[] last = new int[26];
Arrays.fill(last, -1);
for (int i = 0; i < s.length(); i++) last[s.charAt(i) - 'a'] = i;
List ans = new ArrayList<>();
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
end = Math.max(end, last[s.charAt(i) - 'a']);
if (i == end) {
ans.add(end - start + 1);
start = i + 1;
}
}
return ans;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
List parts = partitionLabels(s);
System.out.println(String.join(" ", parts.stream().map(String::valueOf).toArray(String[]::new)));
}
}
#include <bits/stdc++.h>
using namespace std;
vector partitionLabels(const string &s) {
vector last(26, -1);
for (int i = 0; i < (int)s.size(); ++i)
last[s[i] - 'a'] = i;
vector ans;
int start = 0, end = 0;
for (int i = 0; i < (int)s.size(); ++i) {
end = max(end, last[s[i] - 'a']);
if (i == end) {
ans.push_back(end - start + 1);
start = i + 1;
}
}
return ans;
}
int main() {
string s;
getline(cin, s);
vector res = partitionLabels(s);
for (size_t i = 0; i < res.size(); ++i) {
if (i) cout << " ";
cout << res[i];
}
cout << "\n";
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
char s[10005];
if (!fgets(s, sizeof(s), stdin)) return 0;
size_t n = strlen(s);
if (n > 0 && s[n-1] == '\n') { s[n-1] = '\0'; --n; }
int last[26];
for (int i = 0; i < 26; ++i) last[i] = -1;
for (int i = 0; i < (int)n; ++i) last[s[i] - 'a'] = i;
int *res = NULL;
int resCount = 0;
int start = 0, end = 0;
for (int i = 0; i < (int)n; ++i) {
if (last[s[i] - 'a'] > end) end = last[s[i] - 'a'];
if (i == end) {
int len = end - start + 1;
res = (int*)realloc(res, (resCount + 1) * sizeof(int));
res[resCount++] = len;
start = i + 1;
}
}
for (int i = 0; i < resCount; ++i) {
if (i) printf(" ");
printf("%d", res[i]);
}
printf("\n");
free(res);
return 0;
}
using System;
using System.Collections.Generic;
class Solution {
public static IList PartitionLabels(string s) {
int[] last = new int[26];
for (int i = 0; i < 26; i++) last[i] = -1;
for (int i = 0; i < s.Length; i++) last[s[i] - 'a'] = i;
List ans = new List();
int start = 0, end = 0;
for (int i = 0; i < s.Length; i++) {
end = Math.Max(end, last[s[i] - 'a']);
if (i == end) {
ans.Add(end - start + 1);
start = i + 1;
}
}
return ans;
}
static void Main() {
string s = Console.ReadLine();
var parts = PartitionLabels(s);
Console.WriteLine(string.Join(" ", parts));
}
}
