↓Skip to main content
  1. LeetCode/

LeetCode 763: Partition Labels

·2 mins· ·
LeetCode Medium Hash-Table Two-Pointers String Greedy
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-11-03

  • Submissions: Accepted (first try)

Approach
#

A letter can appear in only one part, so a part must extend at least to the last occurrence of every letter inside it (Greedy):

  1. One pass records last, each letter’s last index.
  2. A second pass keeps the current part’s end, end = max(end, last[c]).
  3. When i == end, no letter in the part appears again, so cut here.

Cutting each part as early as possible gives the most parts.

My Attempts
#

Accepted on the first try. My version cut when the next i passed end and appended the last part after the loop. The version below cuts right at i == end, so there is no last part to add separately.

Solution
#

class Solution:
    def partitionLabels(self, s: str) -> List[int]:
        last = {c: i for i, c in enumerate(s)}
        res = []
        start = end = 0
        for i, c in enumerate(s):
            end = max(end, last[c])
            if i == end:              # no letter in this part appears later
                res.append(end - start + 1)
                start = i + 1
        return res

Complexity
#

Time O(n), space O(1) (at most 26 letters).

Mistakes to Avoid
#

  • Using first occurrences: whether you can cut depends on where letters appear last.
  • Deciding the cut from the current letter’s last[c] alone: use the part’s running end, or earlier letters’ ends are ignored.

Interview Explanation
#

A letter can only be in one part, so I first record each letter’s last index. Then I scan the string and extend the current part’s end to the furthest last index of any letter in it; when I reach that end, nothing in the part appears again, so I cut there. Cutting as early as possible gives the most parts. Time O(n), space O(1).

Related

LeetCode 4026: Maximum Gap Between Stations
·2 mins
LeetCode Medium Two-Pointers Greedy String
LeetCode 4026 note: greedily match from the left for each worker’s earliest station and from the right for the latest; splitting between workers i and i+1, the largest gap is latest[i+1] - earliest[i]
LeetCode 244: Shortest Word Distance II
·2 mins
LeetCode Medium Two-Pointers Design Hash-Table String Array
LeetCode 244 note: precompute each word’s positions (sorted by construction), then answer each query by merging the two position lists with two pointers, always advancing the smaller one
LeetCode 1616: Split Two Strings to Make Palindrome
·3 mins
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 note: greedily match a’s prefix against b’s suffix from the outside in; the leftover middle must be a palindrome entirely within a or b, and both directions need checking
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
·2 mins
LeetCode Medium Greedy Hash-Table String Counting
LeetCode 2131 note: pair each word with its reverse on both sides; self-palindromes also pair with each other, and only one leftover can sit in the middle
LeetCode 522: Longest Uncommon Subsequence II
·2 mins
LeetCode Medium Two-Pointers String Sorting Array Hash-Table
LeetCode 522 note: the longest uncommon subsequence is always a whole string; check strings from longest to shortest, and the first that is not a subsequence of any other long-enough string is the answer
LeetCode 4019: Merge Close Characters II
·2 mins
LeetCode Medium Hash-Table String
LeetCode 4019 note: kept characters never change again, so record each character’s last position in the result; a new character within k of it gets merged away