763. Partition Labels LeetCode Solution
In this guide, you will get 763. Partition Labels LeetCode Solution with the best time and space complexity. The solution to Partition Labels problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Partition Labels solution in C++
- Partition Labels solution in Java
- Partition Labels solution in Python
- Additional Resources
Problem Statement of 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”], 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 = “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]
Constraints:
1 <= s.length <= 500
s consists of lowercase English letters.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(128) = O(1)
763. Partition Labels LeetCode Solution in C++
class Solution {
public:
vector<int> partitionLabels(string s) {
vector<int> ans;
vector<int> rightmost(26);
for (int i = 0; i < s.length(); ++i)
rightmost[s[i] - 'a'] = i;
int l = 0; // the leftmost index of the current running string
int r = 0; // the rightmost index of the current running string
for (int i = 0; i < s.length(); ++i) {
r = max(r, rightmost[s[i] - 'a']);
if (r == i) {
ans.push_back(i - l + 1);
l = i + 1;
}
}
return ans;
}
};
/* code provided by PROGIEZ */
763. Partition Labels LeetCode Solution in Java
class Solution {
public List<Integer> partitionLabels(String s) {
List<Integer> ans = new ArrayList<>();
int[] rightmost = new int[26];
for (int i = 0; i < s.length(); ++i)
rightmost[s.charAt(i) - 'a'] = i;
int l = 0; // the leftmost index of the current running string
int r = 0; // the rightmost index of the current running string
for (int i = 0; i < s.length(); ++i) {
r = Math.max(r, rightmost[s.charAt(i) - 'a']);
if (r == i) {
ans.add(i - l + 1);
l = i + 1;
}
}
return ans;
}
}
// code provided by PROGIEZ
763. Partition Labels LeetCode Solution in Python
class Solution:
def partitionLabels(self, s: str) -> list[int]:
ans = []
letterToRightmostIndex = {c: i for i, c in enumerate(s)}
l = 0 # the leftmost index of the current running string
r = 0 # the rightmost index of the current running string
for i, c in enumerate(s):
r = max(r, letterToRightmostIndex[c])
if i == r:
ans.append(r - l + 1)
l = r + 1
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.