[LeetCode] Optimal Partition of String

2405. Optimal Partition of String

Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.

Return the minimum number of substrings in such a partition.

Note that each character should belong to exactly one substring in a partition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int partitionString(string s) {
unordered_set<char> freq;
int res = 1;
for(auto& ch : s) {
if(freq.count(ch)) {
freq.clear();
res++;
}
freq.insert(ch);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/09/11/PS/LeetCode/optimal-partition-of-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.