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