From 5900402de6a7793fba9ba813ad15d59ce0112536 Mon Sep 17 00:00:00 2001 From: realsubodh Date: Mon, 17 Jun 2024 02:47:43 +0530 Subject: [PATCH 1/2] Optimal Partition of String solution in cpp --- cpp/2405-Optimal Partition of String.cpp | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/2405-Optimal Partition of String.cpp diff --git a/cpp/2405-Optimal Partition of String.cpp b/cpp/2405-Optimal Partition of String.cpp new file mode 100644 index 000000000..31d316065 --- /dev/null +++ b/cpp/2405-Optimal Partition of String.cpp @@ -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 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; + } +}; \ No newline at end of file From 33e4921a0008a456553745c4a5af831f65224531 Mon Sep 17 00:00:00 2001 From: Subodh Singh <102369905+realsubodh@users.noreply.github.com> Date: Mon, 17 Jun 2024 02:52:08 +0530 Subject: [PATCH 2/2] Rename 2405-Optimal Partition of String.cpp to 2405-optimal-partition-of-string.cpp --- ...ition of String.cpp => 2405-optimal-partition-of-string.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename cpp/{2405-Optimal Partition of String.cpp => 2405-optimal-partition-of-string.cpp} (99%) diff --git a/cpp/2405-Optimal Partition of String.cpp b/cpp/2405-optimal-partition-of-string.cpp similarity index 99% rename from cpp/2405-Optimal Partition of String.cpp rename to cpp/2405-optimal-partition-of-string.cpp index 31d316065..3a37ecdae 100644 --- a/cpp/2405-Optimal Partition of String.cpp +++ b/cpp/2405-optimal-partition-of-string.cpp @@ -25,4 +25,4 @@ class Solution { return partitionCount; } -}; \ No newline at end of file +};