# Lengths of maximized partitions of a string such that each character of the string appears in one substring

In this article, we will explore the problem of finding the lengths of maximized partitions of a string with unique characters. We will first understand the problem statement and then investigate both the naive and efficient approaches to solve this problem, along with their respective algorithms and time complexities. Lastly, we will implement the solution in C++.

## Problem Statement

Given a string, partition the string into as many substrings as possible such that each character of the string appears in only one substring. Return the lengths of these maximized partitions.

## Naive Approach

The naive approach is to iterate through the string, recording the last occurrence of each character. Then, iterate through the string again and create partitions whenever the current character's last occurrence is found.

## Algorithm (Naive)

• Initialize an array to store the last occurrence of each character in the string.

• Iterate through the string and record the last occurrence of each character.

• Initialize a vector to store the lengths of the partitions.

• Iterate through the string again and create partitions whenever the current character's last occurrence is found.

## C++ Code (Naive)

### Example

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

std::vector<int> partitionLengths(std::string s) {
std::vector<int> lastOccurrence(26, -1);

for (size_t i = 0; i < s.size(); i++) {
lastOccurrence[s[i] - 'a'] = i;
}

std::vector<int> partitionLengths;
int start = 0, end = 0;

for (size_t i = 0; i < s.size(); i++) {
end = std::max(end, lastOccurrence[s[i] - 'a']);
if (i == end) {
partitionLengths.push_back(end - start + 1);
start = i + 1;
}
}

return partitionLengths;
}

int main() {
std::string s = "abacdc";
std::vector<int> lengths = partitionLengths(s);

std::cout << "Lengths of maximized partitions: ";
for (int length : lengths) {
std::cout << length << " ";
}

return 0;
}


### Output

Lengths of maximized partitions: 3 3


Time Complexity (Naive) − O(n), where n is the length of the string.

## Efficient Approach

The efficient approach is similar to the naive approach, but instead of iterating through the string twice, we can create partitions while recording the last occurrence of each character in a single iteration.

## Algorithm (Efficient)

• Initialize an array to store the last occurrence of each character in the string.

• Initialize a vector to store the lengths of the partitions.

• Iterate through the string, record the last occurrence of each character, and create partitions whenever the current character's last occurrence is found.

## C++ Code (Efficient)

Example

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

std::vector<int> partitionLengths(std::string s) {
std::vector<int> lastOccurrence(26, -1);
std::vector<int> partitionLengths;
int start = 0, end = 0;

for (size_t i = 0; i < s.size(); i++) {
lastOccurrence[s[i] - 'a'] = i;
}

for (size_t i = 0; i < s.size(); i++) {
end = std::max(end, lastOccurrence[s[i] - 'a']);

if (i == end) {
partitionLengths.push_back(end - start + 1);
start = i + 1;
}
}

return partitionLengths;
}

int main() {
std::string s = "abacdc";
std::vector<int> lengths = partitionLengths(s);

std::cout << "Lengths of maximized partitions: ";
for (int length : lengths) {
std::cout << length << " ";
}

return 0;
}


### Output

Lengths of maximized partitions: 3 3


Time Complexity (Efficient) − O(n), where n is the length of the string.

## Conclusion

In this article, we explored the problem of finding the lengths of maximized partitions of a string with unique characters. We discussed both the naive and efficient approaches to solve this problem, along with their algorithms and time complexities. The efficient approach, which combines recording the last occurrence of each character and creating partitions in a single iteration, provides an optimized solution. Both approaches have the same time complexity, but the efficient approach uses fewer iterations.

Updated on: 18-May-2023

71 Views 