-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition_labels.py
47 lines (40 loc) · 1.78 KB
/
partition_labels.py
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
# https://leetcode.com/problems/partition-labels/description/
# 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.
# 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.
from typing import List
def partitionLabels(s: str) -> List[int]:
# составляем словарь последних мест появления символа в строке
last = {c: i for i, c in enumerate(s)}
mx = j = 0
ans = []
for i, c in enumerate(s):
# сдвигаем границу интервала
# пока не дошли до символа с последним появлением в строке
mx = max(mx, last[c])
# по достижению границы для группы символов
if mx == i:
# фиксируем длину отрезка символов
ans.append(i - j + 1)
# и назначаем индекс начала следующего списка
j = i + 1
return ans
s = "ababcbacadefegdehijhklij"
# Output: [9,7,8]
# Input: s = "eccbbbbdec"
# Output: [10]
partitionLabels(s)