Skip to content

Commit 0fc1a5c

Browse files
authored
Merge pull request neetcode-gh#3660 from derekjtong/main
Create 2405-optimal-partition-of-string.cpp
2 parents edcf7be + 4df5fb5 commit 0fc1a5c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

cpp/2405-optimal-partition-of-string.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
class Solution {
22
public:
3+
int partitionString(string s) {
4+
vector<int> lastSeen(26, -1);
5+
int count = 1, substringStart = 0;
6+
7+
for (int i = 0; i < s.length(); i++) {
8+
if (lastSeen[s[i] - 'a'] >= substringStart) {
9+
count++;
10+
substringStart = i;
11+
}
12+
lastSeen[s[i] - 'a'] = i;
13+
}
14+
15+
return count;
16+
}
17+
};
18+
19+
20+
class Solution {
21+
public:
322
int minPartitions(std::string s) {
423
// Set to keep track of characters in the current substring
524
std::unordered_set<char> currentChars;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy