Input: s ="accca", k =2Output: 3Explanation:
The optimal way is to change `s[2]` to something other than a and c,forexample, b. then it becomes `"acbca"`.Then we perform the operations:1. The longest prefix containing at most 2 distinct characters is`"ac"`, we remove it and `s` becomes `"bca"`.2. Now The longest prefix containing at most 2 distinct characters is`"bc"`, so we remove it and `s` becomes `"a"`.3. Finally, we remove `"a"` and `s` becomes empty, so the procedure ends.Doing the operations, the string is divided into 3 partitions, so the answer
is3.
Input: s ="aabaab", k =3Output: 1Explanation:
Initially `s` contains 2 distinct characters, so whichever character we
change, it will contain at most 3 distinct characters, so the longest prefix
with at most 3 distinct characters would always be all of it, therefore the
answer is1.
Input: s ="xxyz", k =1Output: 4Explanation:
The optimal way is to change `s[0]` or `s[1]` to something other than
characters in`s`,for example, to change `s[0]` to `w`.Then `s` becomes `"wxyz"`, which consists of 4 distinct characters, so as `k`is1, it will divide into 4 partitions.
To maximize the number of partitions, we want to maximize the number of times we can take a prefix with at most k distinct characters. By changing at most one character, we can try all possible single-character changes and simulate the partitioning process for each, taking the maximum result.
classSolution {
public:int maxPartitionsAfterChange(string s, int k) {
int n = s.size(), ans =0;
auto getPartitions = [&](string t) {
int cnt =0, l =0;
while (l < n) {
unordered_set<char> st;
int r = l;
while (r < n && (st.count(t[r]) || st.size() < k)) {
st.insert(t[r]);
r++;
}
cnt++;
l = r;
}
return cnt;
};
ans = getPartitions(s);
for (int i =0; i < n; ++i) {
for (char c ='a'; c <='z'; ++c) {
if (c == s[i]) continue;
string t = s;
t[i] = c;
ans = max(ans, getPartitions(t));
}
}
return ans;
}
};
classSolution {
publicintmaxPartitionsAfterChange(String s, int k) {
int n = s.length(), ans = 0;
ans = getPartitions(s, k);
for (int i = 0; i < n; i++) {
for (char c ='a'; c <='z'; c++) {
if (c == s.charAt(i)) continue;
StringBuilder t =new StringBuilder(s);
t.setCharAt(i, c);
ans = Math.max(ans, getPartitions(t.toString(), k));
}
}
return ans;
}
privateintgetPartitions(String s, int k) {
int n = s.length(), cnt = 0, l = 0;
while (l < n) {
Set<Character> st =new HashSet<>();
int r = l;
while (r < n && (st.contains(s.charAt(r)) || st.size() < k)) {
st.add(s.charAt(r));
r++;
}
cnt++;
l = r;
}
return cnt;
}
}
classSolution:
defmaxPartitionsAfterChange(self, s: str, k: int) -> int:
defget_partitions(t: str) -> int:
n = len(t)
cnt =0 l =0while l < n:
st = set()
r = l
while r < n and (t[r] in st or len(st) < k):
st.add(t[r])
r +=1 cnt +=1 l = r
return cnt
ans = get_partitions(s)
n = len(s)
for i in range(n):
for c in map(chr, range(97, 123)):
if c == s[i]:
continue t = s[:i] + c + s[i+1:]
ans = max(ans, get_partitions(t))
return ans
impl Solution {
pubfnmax_partitions_after_change(s: String, k: i32) -> i32 {
let n = s.len();
let s = s.as_bytes();
letmut ans = get_partitions(s, k);
for i in0..n {
for c inb'a'..=b'z' {
if c == s[i] { continue; }
letmut t = s.to_vec();
t[i] = c;
ans = ans.max(get_partitions(&t, k));
}
}
ans
}
}
fnget_partitions(s: &[u8], k: i32) -> i32 {
let n = s.len();
letmut cnt =0;
letmut l =0;
while l < n {
letmut st = std::collections::HashSet::new();
letmut r = l;
while r < n && (st.contains(&s[r]) || st.len() < k asusize) {
st.insert(s[r]);
r +=1;
}
cnt +=1;
l = r;
}
cnt
}