Imagine you're playing a word-destruction game where you need to strategically delete parts of a string to maximize your score! šÆ
You are given a string s consisting of only lowercase English letters. In each operation, you can choose one of two actions:
- Delete the entire string
s(this ends the game), or - Delete the first
iletters ofsif the firstiletters match exactly with the followingiletters, where1 ⤠i ⤠s.length / 2
Example: If s = "ababc", you can delete the first 2 letters ("ab") because they match the next 2 letters ("ab"), leaving you with "abc".
Your goal is to find the maximum number of operations needed to completely delete the string. Think of it as maximizing your score by making as many strategic moves as possible!
Input & Output
Visualization
Time & Space Complexity
O(n²) for preprocessing LCP array + O(n²) for DP with each state computed once
O(n²) for LCP array + O(n) for memoization array
Constraints
- 1 ⤠s.length ⤠4000
- s consists of only lowercase English letters
- The string is non-empty