Maximum Deletions on a String

You are given a string s consisting of only lowercase English letters. In one operation, you can:

  • Delete the entire string s, or
  • Delete the first i letters of s if the first i letters of s are equal to the following i letters in s, for any i in the range 1 <= i <= s.length / 2.

Return the maximum number of operations needed to delete all of s.

Example 1
Inputs = "abcabcdabc"
Output2
Delete the first 3 letters because the next 3 letters are equal, then delete all remaining letters, for a maximum of 2 operations.
Example 2
Inputs = "aaabaab"
Output4
By deleting "a", then "aab", then "a", and finally all remaining letters, the string can be deleted in a maximum of 4 operations.

Constraints

  • 1 <= s.length <= 4000
  • s consists only of lowercase English letters.

Asked at 1 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