Minimize String Length

Given a string s, you have two types of operation:

  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if it exists).
  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (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
Inputs = "aaabc"
Output3
By deleting two extra occurrences of a, the string can be minimized to "abc", which has length 3.
Example 2
Inputs = "cbbd"
Output3
By 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

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate