diff --git a/cpp/2304/230404.cpp b/cpp/2304/230404.cpp new file mode 100644 index 0000000..0615da3 --- /dev/null +++ b/cpp/2304/230404.cpp @@ -0,0 +1,29 @@ +#include +#include + +/** + * 2405. Optimal Partition of String + * + * Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once. + * Return the minimum number of substrings in such a partition. + * Note that each character should belong to exactly one substring in a partition. + */ + +class Solution { +public: + static int partitionString(const std::string& s) { + int set = 0, ans = 0; + for (auto&& ch : s) { + if (int c = 1 << (ch - 'a'); set & c) { + set = c; + ++ans; + } else set |= c; + } + return ans + !!set; + } +}; + +int main() { + std::cout << Solution::partitionString("abacaba"); + return 0; +} diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 56933e0..e14ecf6 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -4,4 +4,4 @@ PROJECT(2304) SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_EXPORT_COMPILE_COMMANDS true) -ADD_EXECUTABLE(2304 230404-CN.cpp) +ADD_EXECUTABLE(2304 230404.cpp)