Skip to content

Commit 4f2a683

Browse files
authored
Create 2405-optimal-partition-of-string.cpp
1 parent e984baa commit 4f2a683

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
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+
};

0 commit comments

Comments
 (0)