From 4f2a6833617a34feb1b5b0cf971e135cc55d9464 Mon Sep 17 00:00:00 2001 From: Derek Tong <109226053+derekjtong@users.noreply.github.com> Date: Mon, 7 Oct 2024 14:28:36 -0700 Subject: [PATCH] Create 2405-optimal-partition-of-string.cpp --- cpp/2405-optimal-partition-of-string.cpp | 17 +++++++++++++++++ 1 file changed, 17 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..18b2c4619 --- /dev/null +++ b/cpp/2405-optimal-partition-of-string.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int partitionString(string s) { + vector lastSeen(26, -1); + int count = 1, substringStart = 0; + + for (int i = 0; i < s.length(); i++) { + if (lastSeen[s[i] - 'a'] >= substringStart) { + count++; + substringStart = i; + } + lastSeen[s[i] - 'a'] = i; + } + + return count; + } +};