Advertisement

Partition Labels - LeetCode 763 Solution

Partition Labels - Complete Solution Guide

Partition Labels is LeetCode problem 763, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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"] , but partitions such as ["aba", "bcc"] or ["ab", "ab", "cc"] are invalid. 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 = "ababcba

Detailed Explanation

The problem asks us to divide a given string `s` into the maximum number of partitions such that each character appears in at most one partition. The goal is to return a list of integers where each integer represents the size of a partition. This means we want to find the smallest possible partitions while ensuring no character is split across different partitions. For instance, if a character 'a' appears in indices 0 and 5, any partition that includes index 0 must also include index 5, and all indices in between. The input is a string `s` consisting of lowercase English letters, and the output is a list of the sizes of the resulting partitions.

Solution Approach

The solution uses a greedy approach. First, it calculates the last index of each character in the string using a hash map (or an array if we can assume only lowercase letters). Then, it iterates through the string, maintaining a `start` and `end` pointer for the current partition. For each character, the `end` pointer is updated to the maximum of its current value and the last index of the character encountered. If the current index matches the `end`, it means we've reached the end of a valid partition. We record the size of this partition (end - start + 1) and update the `start` to the beginning of the next potential partition.

Step-by-Step Algorithm

  1. Step 1: Create a data structure (hash map/array) `last_indices` to store the last occurrence index of each character in the string `s`.
  2. Step 2: Initialize an empty list `result` to store the sizes of the partitions.
  3. Step 3: Initialize `start` to 0, representing the start index of the current partition.
  4. Step 4: Initialize `end` to 0, representing the end index of the current partition.
  5. Step 5: Iterate through the string `s` with index `i`.
  6. Step 6: For each character `s[i]`, update `end` to `max(end, last_indices[s[i]])`. This extends the current partition to include all occurrences of `s[i]`.
  7. Step 7: If `i == end`, it means we have reached the end of the current partition. Add `end - start + 1` (the size of the partition) to the `result` list.
  8. Step 8: Update `start` to `i + 1` to start a new partition.
  9. Step 9: Return the `result` list.

Key Insights

  • Insight 1: The key is to find the last occurrence of each character in the string. This helps define the boundaries of each partition.
  • Insight 2: Iterate through the string while keeping track of the 'end' of the current partition. The 'end' is the furthest right position of any character encountered so far in the current partition.
  • Insight 3: When the current index `i` matches the `end` of the partition, it means all characters within the current partition only appear within that partition, so it's time to create a new partition.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Hash Table, Two Pointers, String, Greedy.

Companies

Asked at: InMobi, Sprinklr.