To solve this problem, we need to find the maximum number of non-overlapping "good" substrings from a given string. A "good" substring is defined as one where every character in it does not appear outside the substring. If there are multiple solutions with the same number of substrings, we choose the lexicographically smallest one.
Approach
- Precompute First and Last Occurrences: For each character in the string, compute its first and last occurrence indices. This helps in determining the range of characters that a valid substring must cover.
- Collect Valid Intervals: For each character's first occurrence, expand the interval to include all necessary characters (those whose last occurrence is within the interval). Check if the interval is valid (all characters in the interval have their first occurrence within the interval).
- Dynamic Programming for Optimal Selection: Use dynamic programming to select the maximum number of non-overlapping valid intervals. For each interval, track the maximum count of non-overlapping intervals ending at that interval and the lexicographically smallest list of substrings.
Solution Code
def maxNumOfSubstrings(s):
n = len(s)
first = [-1] * 26
last = [-1] * 26
for i in range(n):
c = ord(s[i]) - ord('a')
if first[c] == -1:
first[c] = i
last[c] = i
valid = []
for i in range(n):
c = ord(s[i]) - ord('a')
if first[c] != i:
continue
r = last[c]
j = i
while j <= r:
curr_c = ord(s[j]) - ord('a')
if last[curr_c] > r:
r = last[curr_c]
j += 1
valid_flag = True
j = i
while j <= r:
curr_c = ord(s[j]) - ord('a')
if first[curr_c] < i:
valid_flag = False
break
j += 1
if valid_flag:
substr = s[i:r+1]
valid.append((r, i, substr))
valid.sort(key=lambda x: (x[0], x[2]))
m = len(valid)
dp = []
max_count = 0
best_list = []
for i in range(m):
r_i, l_i, substr_i = valid[i]
current_count = 1
current_list = [substr_i]
for j in range(i):
r_j, l_j, substr_j = valid[j]
if r_j < l_i:
if dp[j][0] + 1 > current_count:
current_count = dp[j][0] + 1
current_list = dp[j][1] + [substr_i]
elif dp[j][0] + 1 == current_count:
candidate = dp[j][1] + [substr_i]
if candidate < current_list:
current_list = candidate
dp.append((current_count, current_list))
if current_count > max_count:
max_count = current_count
best_list = current_list
elif current_count == max_count:
if current_list < best_list:
best_list = current_list
return best_list
Explanation
- First and Last Occurrences: We use two arrays to store the first and last occurrence of each character. This helps in quickly determining the range of characters that a valid substring must cover.
- Valid Intervals: For each character's first occurrence, we expand the interval to include all characters whose last occurrence is within the interval. We then check if the interval is valid by ensuring all characters in the interval have their first occurrence within the interval.
- Dynamic Programming: We sort the valid intervals by their end and lexicographical order. Using dynamic programming, we track the maximum number of non-overlapping intervals and the lexicographically smallest list of substrings for each interval. This ensures we get the optimal solution.
This approach efficiently finds the desired result with a time complexity dominated by the dynamic programming step, which is O(m^2) where m is the number of valid intervals (up to 26, since there are only 26 lowercase letters). This makes the solution both optimal and efficient.


作者声明:本文包含人工智能生成内容。