1754. 构造字典序最大的合并字符串(Rating 1828)
1754. 构造字典序最大的合并字符串(Rating 1828)
以下内容偏向于记录个人练习过程及思考,非常规题解内容
题目
思路
同时滑动,子字符串字典序更大的优先取即可。
算法复杂度更小的话可以考虑后缀数组。
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def largestMerge(self, word1: str, word2: str) -> str:
m, n = len(word1), len(word2)
i, j = 0, 0
ret = ''
while i < m and j < n:
if word1[i:] > word2[j:]:
ret += word1[i]
i += 1
else:
ret += word2[j]
j += 1
ret += word1[i:]
ret += word2[j:]
return ret
本文由作者按照 CC BY 4.0 进行授权