Minimum Unique-Character Segments After Deletion
Problem statement
You are given a string s containing only lowercase English letters.
Perform exactly one deletion operation:
- Choose any lowercase English letter
removed. The chosen letter does not need to appear ins. - Delete every occurrence of
removedfroms.
After the deletion, partition the remaining string into the minimum possible number of contiguous, nonempty segments such that no letter appears more than once inside any segment.
Return the minimum segment count obtainable over all choices of removed. If the deletion leaves an empty string, return 0.
Function
minSegmentsAfterDeletion(s: String) → intExamples
Example 1
s = "abac"return = 1Choose 'a'. Removing every 'a' leaves "bc", whose letters are distinct, so one segment is enough.
Example 2
s = "abacbc"return = 2Deleting 'a' leaves "bcbc", which can be partitioned as "bc" | "bc". Deleting 'c' similarly leaves "abab". No deletion choice makes every remaining letter distinct, so the answer is 2.
Example 3
s = "aaaa"return = 0Choose 'a'. Every character is deleted, so the remaining empty string needs zero nonempty segments.
Constraints
1 <= s.length <= 2 * 10^5.scontains only lowercase English letters.- The deletion operation chooses one of the 26 lowercase English letters and removes all of its occurrences.