我修改了标题,以使其更易于理解。
这是问题的详细版本:
我们有一个字符串s
,想要将其拆分为子字符串。每个子字符串彼此不同。一次切割最多可以拥有的唯一子字符串的数量是多少。换句话说,串联形成form的唯一子字符串的最大数量是多少s
。
这里有些例子:
Example 1
s = 'aababaa'
output = 4
Explain: we can split `s` into aa|b|aba|a or aab|a|b|aa,
and 4 is the max number of substrings we can get from one split.
Example 2
s = 'aba'
output = 2
Explain: a|ba
Example 3
s = 'aaaaaaa'
output = 3
Explain: a|aa|aaaa
Run Code Online (Sandbox Code Playgroud)
注意:s
仅包含小写字符。我没有被告知需要多长时间s
,因此无法猜测最佳时间复杂度。:(
这是一个NP难题吗?如果没有,如何有效解决?
我从我的一个朋友那里听到了这个问题,无法解决。我正在尝试使用Trie +贪婪解决此问题。对于第一个示例,该方法失败。
这是我想出的Trie解决方案:
Example 1
s = 'aababaa'
output = …
Run Code Online (Sandbox Code Playgroud)