Skip to content

Commit

Permalink
Merge pull request #3498 from realsubodh/main
Browse files Browse the repository at this point in the history
Create: 2405-optimal-partition-of-string.cpp
  • Loading branch information
Ykhan799 authored Nov 3, 2024
2 parents 6aef52e + 33e4921 commit 218c7e0
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cpp/2405-optimal-partition-of-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;
}
};

0 comments on commit 218c7e0

Please sign in to comment.