-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition-labels.js
More file actions
51 lines (41 loc) · 1.11 KB
/
partition-labels.js
File metadata and controls
51 lines (41 loc) · 1.11 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Problem: Partition Labels
* Link: https://leetcode.com/problems/partition-labels/
* Difficulty: Medium
*
* Partition string so each letter appears in at most one part. Return part sizes.
*
* Example: "ababcbacadefegdehijhklij" => [9,7,8]
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
// JavaScript Solution - Greedy
function partitionLabels(s) {
// Record last occurrence of each character
const lastIndex = {};
for (let i = 0; i < s.length; i++) lastIndex[s[i]] = i;
const result = [];
let start = 0, end = 0;
for (let i = 0; i < s.length; i++) {
end = Math.max(end, lastIndex[s[i]]); // extend partition end
if (i === end) {
result.push(end - start + 1); // partition complete
start = i + 1;
}
}
return result;
}
module.exports = partitionLabels;
/* Python Solution:
def partitionLabels(s):
last = {ch: i for i, ch in enumerate(s)}
result = []
start = end = 0
for i, ch in enumerate(s):
end = max(end, last[ch])
if i == end:
result.append(end - start + 1)
start = i + 1
return result
*/