aboutsummaryrefslogtreecommitdiff
path: root/partition_labels.py
blob: 13556c5b198dd18e7bbbf9c9459c11026cf74488 (plain)
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
# 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"]'. 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.
"""


class Solution(object):
    def partitionLabels(self, s):
        """
        :type s: str
        :rtype: List[int]
        """
        curr, ans = 0, [0]
        ends = {c: i for i, c in enumerate(s)}
        while curr < len(s):
            prev = ends[s[curr]]
            while curr <= prev:
                sym = s[curr]
                prev = max(prev, ends[sym])
                curr += 1
            ans.append(curr)
        return [ans[i] - ans[i - 1] for i in range(1, len(ans))]


if __name__ == "__main__":
    obj = Solution()
    print(obj.partitionLabels(s="ababcbacadefegdehijhklij"))
    print(obj.partitionLabels(s="eccbbbbdec"))