Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-04-04 22:00:56
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:
作者: DDFox (冒險者兼清潔工)   2023-04-04 22:16:00
大師

Links booklink

Contact Us: admin [ a t ] ucptt.com