g0701_0800.s0763_partition_labels.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0701_0800.s0763_partition_labels;
// #Medium #String #Hash_Table #Greedy #Two_Pointers #Data_Structure_II_Day_7_String
// #Big_O_Time_O(n)_Space_O(1) #2022_03_26_Time_1_ms_(100.00%)_Space_40.3_MB_(98.19%)
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List partitionLabels(String s) {
char[] letters = s.toCharArray();
List result = new ArrayList<>();
int[] position = new int[26];
for (int i = 0; i < letters.length; i++) {
position[letters[i] - 'a'] = i;
}
int i = 0;
int prev = -1;
int max = 0;
while (i < letters.length) {
if (position[letters[i] - 'a'] > max) {
max = position[letters[i] - 'a'];
}
if (i == max) {
result.add(i - prev);
prev = i;
}
i++;
}
return result;
}
}