Fewest Pieces After Letter Deletion
MediumStrings ยท SalesForce
Given a lowercase string s, choose one letter and delete every occurrence of it from s. The chosen letter may be absent from s.
Split what remains, in order, into contiguous pieces so that no letter appears twice within a piece. Return the smallest number of pieces possible over all choices of letter, or 0 if nothing remains.
Example 1
- Input:
- s = "mississippi"
- Output:
- 4
- Explanation
- Deleting
sleavesmiiippi, which splits intomi,i,ip,pi. Deletingiorpneeds more pieces.
Example 2
- Input:
- s = "abbcbb"
- Output:
- 1
Example 3
- Input:
- s = "zzzz"
- Output:
- 0
Constraints
1 <= s.length <= 10^5scontains only lowercase English letters- The chosen letter is one of the 26 lowercase English letters