插: 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。
坚持不懈,越努力越幸运,大家一起学习鸭~~~
给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目。
字符串 s 拆分后可以得到若干 非空子字符串 ,这些子字符串连接后应当能够还原为原字符串。但是拆分出来的每个子字符串都必须是 唯一的 。
注意:子字符串 是字符串中的一个连续字符序列。
示例 1:
输入:s = “ababccc”
输出:5
解释:一种最大拆分方法为 [‘a’, ‘b’, ‘ab’, ‘c’, ‘cc’] 。像 [‘a’, ‘b’, ‘a’, ‘b’, ‘c’, ‘cc’] 这样拆分不满足题目要求,因为其中的 ‘a’ 和 ‘b’ 都出现了不止一次。
示例 2:
输入:s = “aba”
输出:2
解释:一种最大拆分方法为 [‘a’, ‘ba’] 。
示例 3:
输入:s = “aa”
输出:1
解释:无法进一步拆分字符串。
提示:
1 <= s.length <= 16
class Solution {int maxSplit = 1;public int maxUniqueSplit(String s) {Set set = new HashSet();backtrack(0, 0, s, set);return maxSplit;}public void backtrack(int index, int split, String s, Set set) {int length = s.length();if (index >= length) {maxSplit = Math.max(maxSplit, split);} else {for (int i = index; i < length; i++) {String substr = s.substring(index, i + 1);if (set.add(substr)) {backtrack(i + 1, split + 1, s, set);set.remove(substr);}}}}
}
上一篇:第二类换元法三角代换专项训练
下一篇:C++的IO流