Input: s ="abcdbaefab", k =2Output: trueExplanation:
* We can select two disjoint special substrings:`"cd"` and `"ef"`.*`"cd"` contains the characters `'c'` and `'d'`, which do not appear elsewhere in`s`.*`"ef"` contains the characters `'e'` and `'f'`, which do not appear elsewhere in`s`.
Input: s ="cdefdc", k =3Output: falseExplanation:
There can be at most 2 disjoint special substrings:`"e"` and `"f"`. Since `k
= 3`, the output is`false`.
We want to find the maximum number of disjoint substrings where all characters in each substring are unique to that substring (do not appear elsewhere in the string). This is equivalent to finding the maximum number of disjoint intervals, where each interval covers all occurrences of its characters. We can use a greedy approach similar to the “Partition Labels” problem, but we must ensure substrings are not the entire string.
#include<vector>#include<string>usingnamespace std;
classSolution {
public:bool canSelectKDisjointSpecialSubstrings(string s, int k) {
int n = s.size();
vector<int> first(26, n), last(26, -1);
for (int i =0; i < n; ++i) {
int c = s[i] -'a';
first[c] = min(first[c], i);
last[c] = max(last[c], i);
}
int count =0, i =0;
while (i < n) {
int l = i, r = last[s[i]-'a'];
for (int j = l; j <= r; ++j)
r = max(r, last[s[j]-'a']);
if (l ==0&& r == n-1) { i = r+1; continue; } // skip whole string
count++;
i = r+1;
}
return k ==0|| count >= k;
}
};
classSolution {
publicbooleancanSelectKDisjointSpecialSubstrings(String s, int k) {
int n = s.length();
int[] first =newint[26], last =newint[26];
for (int i = 0; i < 26; ++i) { first[i]= n; last[i]=-1; }
for (int i = 0; i < n; ++i) {
int c = s.charAt(i) -'a';
first[c]= Math.min(first[c], i);
last[c]= Math.max(last[c], i);
}
int count = 0, i = 0;
while (i < n) {
int l = i, r = last[s.charAt(i)-'a'];
for (int j = l; j <= r; ++j)
r = Math.max(r, last[s.charAt(j)-'a']);
if (l == 0 && r == n-1) { i = r+1; continue; }
count++;
i = r+1;
}
return k == 0 || count >= k;
}
}
classSolution {
funcanSelectKDisjointSpecialSubstrings(s: String, k: Int): Boolean {
val n = s.length
val first = IntArray(26) { n }
val last = IntArray(26) { -1 }
for (i in s.indices) {
val c = s[i] - 'a' first[c] = minOf(first[c], i)
last[c] = maxOf(last[c], i)
}
var count = 0var i = 0while (i < n) {
var l = i
var r = last[s[i] - 'a']
var j = l
while (j <= r) {
r = maxOf(r, last[s[j] - 'a'])
j++ }
if (l ==0&& r == n-1) { i = r+1; continue }
count++ i = r+1 }
return k ==0|| count >= k
}
}
defcanSelectKDisjointSpecialSubstrings(s: str, k: int) -> bool:
n = len(s)
first = [n]*26 last = [-1]*26for i, c in enumerate(s):
idx = ord(c) - ord('a')
first[idx] = min(first[idx], i)
last[idx] = max(last[idx], i)
count =0 i =0while i < n:
l = i
r = last[ord(s[i])-ord('a')]
j = l
while j <= r:
r = max(r, last[ord(s[j])-ord('a')])
j +=1if l ==0and r == n-1:
i = r+1continue count +=1 i = r+1return k ==0or count >= k
pubfncan_select_k_disjoint_special_substrings(s: &str, k: i32) -> bool {
let n = s.len();
let s = s.as_bytes();
letmut first =vec![n; 26];
letmut last =vec![-1; 26];
for (i, &c) in s.iter().enumerate() {
let idx = (c -b'a') asusize;
if i < first[idx] { first[idx] = i; }
if i asi32> last[idx] { last[idx] = i asi32; }
}
letmut count =0;
letmut i =0;
while i < n {
let l = i;
letmut r = last[(s[i] -b'a') asusize] asusize;
letmut j = l;
while j <= r {
r = r.max(last[(s[j] -b'a') asusize] asusize);
j +=1;
}
if l ==0&& r == n-1 { i = r+1; continue; }
count +=1;
i = r+1;
}
k ==0|| count >= k
}