Skip to content

Files

Latest commit

aac7862 · Nov 17, 2022

History

History

763-PartitionLabels

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Nov 17, 2022
Nov 17, 2022

Partition Labels

Problem can be found in here!

Solution: Greedy

def partitionLabels(s: str) -> List[int]:
    answer = []
    end_position = last_position = 0
    memo = {char: index for index, char in enumerate(s)}

    for index, char in enumerate(s):
        end_position = max(end_position, memo[char])
        if index == end_position:
            answer.append(index-last_position+1)
            last_position = index + 1

    return answer

Time Complexity: O(n), Space Complexity: O(1)