# 2405. Optimal Partition of String ###### tags: `Leetcode` `Medium` `Greedy` Link: https://leetcode.com/problems/optimal-partition-of-string/ ## 思路 每次都找最长的没有重复字母的substring就可以得到optimal partition ## Code ```java= class Solution { public int partitionString(String s) { Set<Character> set = new HashSet<>(); char[] arr = s.toCharArray(); int ans=1; for(int i=0; i<s.length(); i++){ if(set.contains(arr[i])){ ans+=1; set.clear(); } set.add(arr[i]); } return ans; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up