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.
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 <= s.length <= 500
s
consists of lowercase English letters.
class Solution:
def partitionLabels(self, s: str) -> List[int]:
last = [0] * 26
for i, c in enumerate(s):
last[ord(c) - ord('a')] = i
ans = []
left = right = 0
for i, c in enumerate(s):
right = max(right, last[ord(c) - ord('a')])
if i == right:
ans.append(right - left + 1)
left = right + 1
return ans
class Solution {
public List<Integer> partitionLabels(String s) {
int[] last = new int[26];
int n = s.length();
for (int i = 0; i < n; ++i) {
last[s.charAt(i) - 'a'] = i;
}
List<Integer> ans = new ArrayList<>();
for (int i = 0, left = 0, right = 0; i < n; ++i) {
right = Math.max(right, last[s.charAt(i) - 'a']);
if (i == right) {
ans.add(right - left + 1);
left = right + 1;
}
}
return ans;
}
}
function partitionLabels(s: string): number[] {
const n = s.length;
let last = new Array(26);
for (let i = 0; i < n; i++) {
last[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
}
let ans = [];
let left = 0,
right = 0;
for (let i = 0; i < n; i++) {
right = Math.max(right, last[s.charCodeAt(i) - 'a'.charCodeAt(0)]);
if (i == right) {
ans.push(right - left + 1);
left = right + 1;
}
}
return ans;
}
class Solution {
public:
vector<int> partitionLabels(string s) {
vector<int> last(26);
int n = s.size();
for (int i = 0; i < n; ++i) last[s[i] - 'a'] = i;
vector<int> ans;
for (int i = 0, left = 0, right = 0; i < n; ++i)
{
right = max(right, last[s[i] - 'a']);
if (i == right)
{
ans.push_back(right - left + 1);
left = right + 1;
}
}
return ans;
}
};
func partitionLabels(s string) []int {
last := make([]int, 26)
n := len(s)
for i := 0; i < n; i++ {
last[s[i]-'a'] = i
}
var ans []int
for i, left, right := 0, 0, 0; i < n; i++ {
right = max(right, last[s[i]-'a'])
if i == right {
ans = append(ans, right-left+1)
left = right + 1
}
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
impl Solution {
pub fn partition_labels(s: String) -> Vec<i32> {
let n = s.len();
let bytes = s.as_bytes();
let mut inx_arr = [0; 26];
for i in 0..n {
inx_arr[(bytes[i] - b'a') as usize] = i;
}
let mut res = vec![];
let mut left = 0;
let mut right = 0;
for i in 0..n {
right = right.max(inx_arr[(bytes[i] - b'a') as usize]);
if right == i {
res.push((right - left + 1) as i32);
left = i + 1;
}
}
res
}
}