-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
2405-optimal-partition-of-string.cpp
47 lines (40 loc) · 1.34 KB
/
2405-optimal-partition-of-string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class Solution {
public:
int partitionString(string s) {
vector<int> lastSeen(26, -1);
int count = 1, substringStart = 0;
for (int i = 0; i < s.length(); i++) {
if (lastSeen[s[i] - 'a'] >= substringStart) {
count++;
substringStart = i;
}
lastSeen[s[i] - 'a'] = i;
}
return count;
}
};
class Solution {
public:
int minPartitions(std::string s) {
// Set to keep track of characters in the current substring
std::unordered_set<char> currentChars;
// Variable to count the number of partitions
int partitionCount = 0;
// Iterate over each character in the string
for (char c : s) {
// If the character is already in the set, it means we've encountered a duplicate
if (currentChars.find(c) != currentChars.end()) {
// Increment the partition count and start a new substring
partitionCount++;
currentChars.clear();
}
// Add the current character to the set
currentChars.insert(c);
}
// There will be at least one partition at the end if currentChars is not empty
if (!currentChars.empty()) {
partitionCount++;
}
return partitionCount;
}
};