File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments