-
Notifications
You must be signed in to change notification settings - Fork 0
/
partitionLabels.js
35 lines (30 loc) · 923 Bytes
/
partitionLabels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var partitionLabels = function(s) {
let startIndex = -1, freqMap = {};
for(let itr = 0 ; itr < s.length ; itr++) {
let ch = s[itr];
!freqMap.hasOwnProperty(ch) && (freqMap[ch] = 0);
freqMap[ch]++;
}
let tempHashMap = {}, arr = [], distinctCharCount = 0;
for(let itr = 0 ; itr < s.length ; itr++) {
let ch = s[itr];
if(startIndex == -1) {
startIndex = itr;
}
if(!tempHashMap[ch]){
distinctCharCount++;
}
tempHashMap[ch] = 1;
freqMap[ch]--;
if(freqMap[ch] == 0 ) {
delete tempHashMap[ch];
distinctCharCount--;
if(distinctCharCount == 0) {
arr.push(itr - startIndex + 1);
startIndex= -1
}
}
}
return arr;
};
// https://leetcode.com/problems/partition-labels/