Medium
参考了大佬们的思路
以上,尝试写一下代码,AC!
python3
class Solution:
def maxNumOfSubstrings(self, s: str) -> List[str]:
chars = {}
for i,c in enumerate(s):
if c not in chars:
chars[c] = [i,i+1]
else:
chars[c][1] = i+1
#扩展字母的范围
lrange = []
for start, end in chars.values():
i = start + 1
max_end = end
while i < max_end: # 不断往后拓展区间,以满足条件 2
if chars[s[i]][0] < start: # 不往前拓展
break
max_end = max(max_end, chars[s[i]][1])
i += 1
else:
lrange.append([start, max_end])
lrange.sort()
prer = [0, 0]
res = []
for r in lrange:
left,right = r[0], r[1]
if right <= prer[1] and len(res):
res.pop(-1)
res.append(s[left:right])
prer = [left, right]
return res