Skip to content

Commit 511f16a

Browse files
committed
Add solution #763
1 parent ce6ae8e commit 511f16a

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@
421421
747|[Largest Number At Least Twice of Others](./0747-largest-number-at-least-twice-of-others.js)|Easy|
422422
748|[Shortest Completing Word](./0748-shortest-completing-word.js)|Easy|
423423
762|[Prime Number of Set Bits in Binary Representation](./0762-prime-number-of-set-bits-in-binary-representation.js)|Easy|
424+
763|[Partition Labels](./0763-partition-labels.js)|Medium|
424425
784|[Letter Case Permutation](./0784-letter-case-permutation.js)|Medium|
425426
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|
426427
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|

solutions/0763-partition-labels.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 763. Partition Labels
3+
* https://leetcode.com/problems/partition-labels/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s. We want to partition the string into as many parts as
7+
* possible so that each letter appears in at most one part. For example, the
8+
* string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as
9+
* ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid.
10+
*
11+
* Note that the partition is done so that after concatenating all the parts in
12+
* order, the resultant string should be s.
13+
*
14+
* Return a list of integers representing the size of these parts.
15+
*/
16+
17+
/**
18+
* @param {string} s
19+
* @return {number[]}
20+
*/
21+
var partitionLabels = function(s) {
22+
const map = new Map();
23+
for (let i = 0; i < s.length; i++) {
24+
map.set(s[i], i);
25+
}
26+
27+
const result = [];
28+
for (let i = 0, start = 0, end = 0; i < s.length; i++) {
29+
end = Math.max(end, map.get(s[i]));
30+
if (i === end) {
31+
result.push(end - start + 1);
32+
start = i + 1;
33+
}
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)