-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2405-Optimal_Partition_of_String.cpp
More file actions
78 lines (70 loc) · 2.02 KB
/
2405-Optimal_Partition_of_String.cpp
File metadata and controls
78 lines (70 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*******************************************************************************
* 2405-Optimal_Partition_of_String.cpp
* Billy.Ljm
* 04 Apr 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* The optimal approach is the greedy approach: if a character is not already in
* the current substring, force it into the substring; otherwise, a new
* substring/partition has to be created.
*
* It has a time complexity of O(n) and a space complexity of O(1), where n is
* the length of the string.
******************************************************************************/
#include <iostream>
#include <string>
#include <vector>
class Solution {
public:
/**
* Finds minimum number of partitions in a string to ensure each substring
* has no repeating characters
*
* @param s string to partition
*
* @return minimum number of partitions
*/
int partitionString(std::string s) {
std::vector<int> seen(26, -1); // seen[0] = index of last seen 'a'
int hash = 0; // hash of current char; 'a' to 0, 'b' to 1, etc.
int currindex = 0; // starting index of current partition
int numpartition = 1; // number of partitions
for (int i = 0; i < s.size(); i++) {
hash = int(s[i]) - 97;
if (seen[hash] >= currindex) { // new partition to be created
currindex = i;
numpartition++;
}
seen[hash] = i;
}
return numpartition;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
std::string s;
// test case 1
s = "abacaba";
std::cout << "partitionString(" << s << ") = " <<
sol.partitionString(s) << std::endl;
// test case 2
s = "ssssss";
std::cout << "partitionString(" << s << ") = " <<
sol.partitionString(s) << std::endl;
return 0;
}