Tuesday, January 8, 2019

763. Partition Labels

Version #2 Two Pointers? [TODO]
Why

Version #1 Straight forward
Using hashset to keep track of uncleared characters

class Solution {
    public List<Integer> partitionLabels(String S) {
List<Integer> result = new ArrayList<>();
int[] count = new int[26];
for (int i = 0; i < S.length(); i++) {;
count[S.charAt(i) - 'a']++;
}
Set<Character> set = new HashSet<>();
// "ababcbacadefegdehijhklij"
int p = 0;
while (p < S.length()) {
int counter = 0;
do {
char c = S.charAt(p);
if (--count[c - 'a'] > 0) {
set.add(c);
} else {
set.remove(c);
}
p++;
counter++;
} while (!set.isEmpty());
result.add(counter);
}
return result;
}
}

No comments:

Post a Comment