Skip to content

Commit 218c7e0

Browse files
authored
Merge pull request #3498 from realsubodh/main
Create: 2405-optimal-partition-of-string.cpp
2 parents 6aef52e + 33e4921 commit 218c7e0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
int minPartitions(std::string s) {
4+
// Set to keep track of characters in the current substring
5+
std::unordered_set<char> currentChars;
6+
// Variable to count the number of partitions
7+
int partitionCount = 0;
8+
9+
// Iterate over each character in the string
10+
for (char c : s) {
11+
// If the character is already in the set, it means we've encountered a duplicate
12+
if (currentChars.find(c) != currentChars.end()) {
13+
// Increment the partition count and start a new substring
14+
partitionCount++;
15+
currentChars.clear();
16+
}
17+
// Add the current character to the set
18+
currentChars.insert(c);
19+
}
20+
21+
// There will be at least one partition at the end if currentChars is not empty
22+
if (!currentChars.empty()) {
23+
partitionCount++;
24+
}
25+
26+
return partitionCount;
27+
}
28+
};

0 commit comments

Comments
 (0)