Minimize String Length
Given a string s, you have two types of operation:
- Choose an index
iin the string, and letcbe the character in positioni. Delete the closest occurrence ofcto the left ofi(if it exists). - Choose an index
iin the string, and letcbe the character in positioni. Delete the closest occurrence ofcto the right ofi(if it exists).
Your task is to minimize the length of s by performing the above operations zero or more times.
Return an integer denoting the length of the minimized string.
Example 1
Input
s = "aaabc"Output
3By deleting two extra occurrences of
a, the string can be minimized to "abc", which has length 3.Example 2
Input
s = "cbbd"Output
3By deleting one extra occurrence of
b, the string can be minimized to "cbd", which has length 3.Constraints
- 1 <= s.length <= 100
- s contains only lowercase English letters