763. Partition Labels

Difficulty: Medium 

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]
Python SOlution:
class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        l=[0]*26
        for i in range(len(s)):
            l[ord(s[i])-ord('a')]=i
        start,end=0,0
        lst=[]
        for i in range(len(s)):
            end=max(end,l[ord(s[i])-ord('a')])
            if i==end:
                lst.append(end-start+1)
                start=end+1
        return lst

Comments

Popular posts from this blog

35. Search Insert Position

118. Pascal's Triangle