Given a string `s`

of lowercase letters, you need to find the maximum number of **non-empty** substrings of `s`

that meet the following conditions:

- The substrings do not overlap, that is for any two substrings
`s[i..j]`

and`s[k..l]`

, either`j < k`

or`i > l`

is true. - A substring that contains a certain character
`c`

must also contain all occurrences of`c`

.

Find *the maximum number of substrings that meet the above conditions*. If there are multiple solutions with the same number of substrings, *return the one with minimum total length. *It can be shown that there exists a unique solution of minimum total length.

Notice that you can return the substrings in **any** order.

**Example 1:**

Input:s = "adefaddaccc"Output:["e","f","ccc"]Explanation:The following are all the possible substrings that meet the conditions: [ "adefaddaccc" "adefadda", "ef", "e", "f", "ccc", ] If we choose the first string, we cannot choose anything else and we'd get only 1. If we choose "adefadda", we are left with "ccc" which is the only one that doesn't overlap, thus obtaining 2 substrings. Notice also, that it's not optimal to choose "ef" since it can be split into two. Therefore, the optimal way is to choose ["e","f","ccc"] which gives us 3 substrings. No other solution of the same number of substrings exist.

**Example 2:**

Input:s = "abbaccd"Output:["d","bb","cc"]Explanation:Notice that while the set of substrings ["d","abba","cc"] also has length 3, it's considered incorrect since it has larger total length.

**Constraints:**

`1 <= s.length <= 10^5`

`s`

contains only lowercase English letters.

**Solution: Greedy**

Observation: If a valid substring contains shorter valid strings, ignore the longer one and use the shorter one.

e.g. “abbeefba” is a valid substring, however, it includes “bbeefb”, “ee”, “f” three valid substrings, thus it won’t be part of the optimal solution, since we can always choose a shorter one, with potential to have one or more non-overlapping substrings. For “bbeefb”, again it includes “ee” and “f”, so it won’t be optimal either. Thus, the optimal ones are “ee” and “f”.

- We just need to record the first and last occurrence of each character
- When we meet a character for the first time we must include everything from current pos to it’s last position. e.g. “
**a**bbeefb**a**” | ccc, from first ‘a’ to last ‘a’, we need to cover “abbeefba” - If any character in that range has larger end position, we must extend the string. e.g. “
**a**bc**a**bbcc” | efg, from first ‘a’ to last ‘a’, we have characters ‘b’ and ‘c’, so we have to extend the string to cover all ‘b’s and ‘c’s. Our first valid substring extended from “abca” to “abcabbcc”. - If any character in the covered range has a smallest first occurrence, then it’s an invalid substring. e.g. ab | “cbc”, from first ‘c’ to last ‘c’, we have ‘b’, but ‘b’ is not fully covered, thus “cbc” is an invalid substring.
- For the first valid substring, we append it to the ans array. “abbeefba” => ans = [“abbeefba”]
- If we find a shorter substring that is full covered by the previous valid substring, we replace that substring with the shorter one. e.g.

“abbeefba” | ccc => ans = [“abbeefba”]

“a**bbeefb**a” | ccc => ans = [“bbeefb”]

“abb**ee**fba” | ccc => ans = [“ee”] - If the current substring does not overlap with previous one, append it to ans array.

“abb**ee**fba” | ccc => ans = [“ee”]

“abbee**f**ba” | ccc => ans = [“ee”, “f”]

“abbeefba**ccc**” => ans = [“ee”, “f”, “ccc”]

Time complexity: O(n)

Space complexity: O(1)

## C++

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 |
// Author: Huahua class Solution { public: vector<string> maxNumOfSubstrings(const string& s) { const int n = s.length(); vector<int> l(26, INT_MAX); vector<int> r(26, INT_MIN); for (int i = 0; i < n; ++i) { l[s[i] - 'a'] = min(l[s[i] - 'a'], i); r[s[i] - 'a'] = max(r[s[i] - 'a'], i); } auto extend = [&](int i) -> int { int p = r[s[i] - 'a']; for (int j = i; j <= p; ++j) { if (l[s[j] - 'a'] < i) // invalid substring return -1; // e.g. a|"ba"...b p = max(p, r[s[j] - 'a']); } return p; }; vector<string> ans; int last = -1; for (int i = 0; i < n; ++i) { if (i != l[s[i] - 'a']) continue; int p = extend(i); if (p == -1) continue; if (i > last) ans.push_back(""); ans.back() = s.substr(i, p - i + 1); last = p; } return ans; } }; |

请尊重作者的劳动成果，转载请注明出处！花花保留对文章／视频的所有权利。

如果您喜欢这篇文章／视频，欢迎您捐赠花花。

If you like my articles / videos, donations are welcome.

## Be First to Comment