class Solution:
def maxNumOfSubstrings(self, s: str) -> list:
n = len(s)
first = [n] * 26
last = [-1] * 26
for i, c in enumerate(s):
idx = ord(c) - ord('a')
first[idx] = min(first[idx], i)
last[idx] = max(last[idx], i)
intervals = []
for i in range(26):
if first[i] <= last[i]:
l, r = first[i], last[i]
j = l
while j <= r:
idx2 = ord(s[j]) - ord('a')
l = min(l, first[idx2])
r = max(r, last[idx2])
j += 1
intervals.append((l, r))
intervals.sort(key=lambda x: x[1])
res = []
end = -1
for l, r in intervals:
if l > end:
res.append(s[l:r+1])
end = r
return res
class Solution {
public:
vector<string> maxNumOfSubstrings(string s) {
int n = s.size();
vector<int> first(26, n), last(26, -1);
for (int i = 0; i < n; ++i) {
int idx = s[i] - 'a';
first[idx] = min(first[idx], i);
last[idx] = max(last[idx], i);
}
vector<pair<int, int>> intervals;
for (int i = 0; i < 26; ++i) {
if (first[i] <= last[i]) {
int l = first[i], r = last[i];
int j = l;
while (j <= r) {
int idx2 = s[j] - 'a';
l = min(l, first[idx2]);
r = max(r, last[idx2]);
++j;
}
intervals.push_back({l, r});
}
}
sort(intervals.begin(), intervals.end(), [](auto& a, auto& b){ return a.second < b.second; });
vector<string> res;
int end = -1;
for (auto& p : intervals) {
if (p.first > end) {
res.push_back(s.substr(p.first, p.second - p.first + 1));
end = p.second;
}
}
return res;
}
};
class Solution {
public List<String> maxNumOfSubstrings(String s) {
int n = s.length();
int[] first = new int[26];
int[] last = new int[26];
Arrays.fill(first, n);
Arrays.fill(last, -1);
for (int i = 0; i < n; i++) {
int idx = s.charAt(i) - 'a';
first[idx] = Math.min(first[idx], i);
last[idx] = Math.max(last[idx], i);
}
List<int[]> intervals = new ArrayList<>();
for (int i = 0; i < 26; i++) {
if (first[i] <= last[i]) {
int l = first[i], r = last[i];
int j = l;
while (j <= r) {
int idx2 = s.charAt(j) - 'a';
l = Math.min(l, first[idx2]);
r = Math.max(r, last[idx2]);
j++;
}
intervals.add(new int[]{l, r});
}
}
intervals.sort(Comparator.comparingInt(a -> a[1]));
List<String> res = new ArrayList<>();
int end = -1;
for (int[] p : intervals) {
if (p[0] > end) {
res.add(s.substring(p[0], p[1] + 1));
end = p[1];
}
}
return res;
}
}
var maxNumOfSubstrings = function(s) {
const n = s.length;
const first = Array(26).fill(n);
const last = Array(26).fill(-1);
for (let i = 0; i < n; i++) {
let idx = s.charCodeAt(i) - 97;
first[idx] = Math.min(first[idx], i);
last[idx] = Math.max(last[idx], i);
}
let intervals = [];
for (let i = 0; i < 26; i++) {
if (first[i] <= last[i]) {
let l = first[i], r = last[i], j = l;
while (j <= r) {
let idx2 = s.charCodeAt(j) - 97;
l = Math.min(l, first[idx2]);
r = Math.max(r, last[idx2]);
j++;
}
intervals.push([l, r]);
}
}
intervals.sort((a, b) => a[1] - b[1]);
let res = [];
let end = -1;
for (let [l, r] of intervals) {
if (l > end) {
res.push(s.substring(l, r + 1));
end = r;
}
}
return res;
};
s
, such that each substring contains all occurrences of every character it contains.
s
.1 <= s.length <= 1000
s
consists only of lowercase English letters.s
.s
.s
and return the list.s = "adefaddaccc"
["e","f","ccc"]
(order may vary)