-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path30_Partition_Labels.cpp
64 lines (50 loc) · 2.03 KB
/
30_Partition_Labels.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
// 763. Partition Labels
// You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part. For example, the string "ababcc" can be partitioned into ["abab", "cc"], but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid.
// Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.
// Return a list of integers representing the size of these parts.
// Example 1:
// Input: s = "ababcbacadefegdehijhklij"
// Output: [9,7,8]
// Explanation:
// The partition is "ababcbaca", "defegde", "hijhklij".
// This is a partition so that each letter appears in at most one part.
// A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.
// Example 2:
// Input: s = "eccbbbbdec"
// Output: [10]
// Constraints:
// 1 <= s.length <= 500
// s consists of lowercase English letters.
class Solution
{
public:
vector<int> partitionLabels(string s)
{
unordered_map<char, int> last_occurrence;
for (int i = 0; i < s.size(); i++)
{
last_occurrence[s[i]] = i;
}
vector<int> result;
int start = 0, end = 0;
for (int i = 0; i < s.size(); i++)
{
end = max(end, last_occurrence[s[i]]);
if (i == end)
{
result.push_back(end - start + 1);
start = i + 1;
}
}
return result;
}
};
/*
1. Create a map to store the last occurrence of each character
2. Iterate through the string
3. For each position, update the end boundary of the current partition based on the last occurrence of the current character
4. If we've reached the end boundary of our current partition, it means we've included all necessary characters for this partition, so we add its length to our result
Complexity
Time complexity: O(n), where n is the length of the string
Space complexity: O(k), where k is the number of unique characters in the string
*/