You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
603 B

2 years ago
package class38;
import java.util.ArrayList;
import java.util.List;
public class Problem_0763_PartitionLabels {
public static List<Integer> partitionLabels(String S) {
char[] str = S.toCharArray();
int[] far = new int[26];
for (int i = 0; i < str.length; i++) {
far[str[i] - 'a'] = i;
}
List<Integer> ans = new ArrayList<>();
int left = 0;
int right = far[str[0] - 'a'];
for (int i = 1; i < str.length; i++) {
if (i > right) {
ans.add(right - left + 1);
left = i;
}
right = Math.max(right, far[str[i] - 'a']);
}
ans.add(right - left + 1);
return ans;
}
}