2405. Optimal Partition of String
給定一個字串s,我們想將 s 切成 n 個子字串,這些子字串不可以存在重複的字母,
求最少需要切成幾個子字串。
Example:
Input: s = "abacaba"
Output: 4
Explanation:
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
Input: s = "ssssss"
Output: 6
Explanation:
The only valid partition is ("s","s","s","s","s","s").
思路:
1.res從1開始,因為最少存在一個字串。
2.統計當前切法的字母數量,若當前子字串加入新字元前發現重複,就開一個新的子字
串並讓res遞增,遍歷完一遍就完事了。
Java Code: