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:
s[i..j]
and s[x..y]
, either j < x
or i > y
is true.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"]
Example 2:
Input: s = "abbaccd"
Output: ["d","bb","cc"]
Constraints:
1 <= s.length <= 10^5
s
contains only lowercase English letters.def max_substrings(s):
"""Finds the maximum number of non-overlapping substrings of s meeting the given conditions."""
n = len(s)
if n == 0:
return []
# 1. Find the first and last occurrence of each character.
first_occurrence = {}
last_occurrence = {}
for i, char in enumerate(s):
if char not in first_occurrence:
first_occurrence[char] = i
last_occurrence[char] = i
# 2. Merge intervals.
intervals = []
for char in first_occurrence:
intervals.append([first_occurrence[char], last_occurrence[char]])
i = 0
while i < len(intervals):
j = 0
while j < len(intervals):
if i != j:
# Check if intervals overlap
if intervals[j][0] >= intervals[i][0] and intervals[j][1] <= intervals[i][1]:
# Check if all occurrences of characters in intervals[j] are within intervals[i]
valid_merge = True
for char in first_occurrence:
if first_occurrence[char] >= intervals[j][0] and first_occurrence[char] <= intervals[j][1]:
if first_occurrence[char] < intervals[i][0] or first_occurrence[char] > intervals[i][1] or \
last_occurrence[char] < intervals[i][0] or last_occurrence[char] > intervals[i][1]:
valid_merge = False
break
if valid_merge:
intervals.pop(j)
if j < i:
i -= 1
j = -1 # Restart the inner loop
if j >= 0:
j += 1
i += 1
# Sort intervals by end position.
intervals.sort(key=lambda x: x[1])
# Select non-overlapping intervals.
result = []
end = -1
for start, current_end in intervals:
if start > end:
result.append(s[start:current_end + 1])
end = current_end
return result
# Example usage
s = "adefaddaccc"
print(max_substrings(s))
s = "abbaccd"
print(max_substrings(s))
The code implements a greedy algorithm to find the maximum number of non-overlapping substrings satisfying the given conditions.
s
. This information is crucial for determining the boundaries of potential substrings.end
variable keeps track of the end position of the last selected interval, ensuring that only non-overlapping intervals are chosen.