---
# System prepended metadata

title: 2405. Optimal Partition of String
tags: [Leetcode, Medium, Greedy]

---

# 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;
    }
}
```