-
Notifications
You must be signed in to change notification settings - Fork 1
/
2405_optimal_partition_in_string.cpp
131 lines (109 loc) · 2.7 KB
/
2405_optimal_partition_in_string.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<bits/stdc++.h>
using namespace std;
/*
2405. Optimal Partition of String
Medium
690
33
Companies
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.
Example 1:
Input: s = "abacaba"
Output: 4
Explanation:
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
*/
// my solution
class Solution {
bool isUnique(string &s,int i,int j)
{
// unordered_map<char,int> m;
unordered_set<char> hs;
for(int k=i;k<=j;k++)
{
// m[s[k]]++;
// if(m[s[k]] > 1)
// {
// return false;
// }
if(hs.find(s[k]) != hs.end())
{
return false;
}
hs.insert(s[k]);
}
return true;
}
public:
int partitionString(string s) {
int i=0,j=0,n=s.size();
int count=0;
while(j<n)
{
if(isUnique(s,i,j))
{
j++;
}else{
i=j;
count++;
}
}
if(isUnique(s,i,j-1))
count++;
return count;
}
};
// most easy to understand and more efficient then my sol.
class Solution {
public:
int partitionString(string s) {
unordered_map<char,int> umap;
int ct=0;
for(auto i:s){
if(umap.count(i)==0)
umap[i]++;
else{
ct+=1;
umap.clear();
umap[i]++;
}
}
ct+=1;
return ct;
}
};
// solution of some MAD guy -> most optimal.
int partitionString(string s) {
int flag=0,ans=1;
for(int i=0;i<s.size();i++)
{
int k=s[i]-'a';
if(flag&(1<<k))
{
ans++;
flag=0;
}
flag=flag|(1<<k);
}
return ans;
}
// ultimate MAD guy.
// code not at all easy to understand
const static auto initialize = [] { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return nullptr; }();
class Solution
{
public:
int partitionString(std::string const& s)
{
return std::accumulate(std::cbegin(s), std::cend(s), 1,
[observed = std::array<int, 128>{0}](auto count, auto c) mutable
{
if (observed[c] == count) { ++count; }
observed[c] = count;
return count;
});
}
};