Maximum Deletions on a String - Problem

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 i letters of s if the first i letters match exactly with the following i letters, where 1 ≤ 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

example_1.py — Basic Case
$ Input: s = "abcabcdabc"
› Output: 2
šŸ’” Note: First operation: delete "abc" (first 3 letters match next 3 letters), leaving "abcdabc". Second operation: delete the entire remaining string "abcdabc". Total operations = 2.
example_2.py — Repeated Pattern
$ Input: s = "ababab"
› Output: 3
šŸ’” Note: First: delete "ab" leaving "abab". Second: delete "ab" leaving "ab". Third: delete remaining "ab". Total = 3 operations.
example_3.py — No Valid Prefix
$ Input: s = "abcdef"
› Output: 1
šŸ’” Note: No prefix matches the following substring of same length, so we can only delete the entire string in 1 operation.

Visualization

Tap to expand
Maximum String Deletions StrategyInput String: "ababab"Position:0 1 2 3 4 5Character:a b a b a bStep 1: Delete "ab"Check: s[0:2] vs s[2:4]"ab" == "ab" āœ“Valid deletion!Remaining: "abab"Operations so far: 1Step 2: Delete "ab"Check: "ab" vs "ab"Match found āœ“Valid deletion!Remaining: "ab"Operations so far: 2Step 3: Delete AllString: "ab"Length = 2No valid prefixMust delete entirelyFinal operations: 3šŸŽÆ Optimal StrategyUse DP + MemoizationPrecompute LCP for O(1) comparisonsTime: O(n²), Space: O(n²)
Understanding the Visualization
1
Identify Valid Peels
Find all positions where the prefix matches the following substring of equal length
2
Recursive Exploration
For each valid peel, recursively solve for the remaining string after the deletion
3
Memoization
Cache results to avoid recalculating the same subproblems multiple times
4
Optimal Choice
Choose the deletion strategy that maximizes the total number of operations
Key Takeaway
šŸŽÆ Key Insight: The optimal approach uses dynamic programming with memoization, combined with LCP preprocessing for efficient string matching. Each position represents a state where we maximize operations by trying all valid prefix deletions.

Time & Space Complexity

Time Complexity
ā±ļø
O(n²)

O(n²) for preprocessing LCP array + O(n²) for DP with each state computed once

n
2n
⚠ Quadratic Growth
Space Complexity
O(n²)

O(n²) for LCP array + O(n) for memoization array

n
2n
⚠ Quadratic Space

Constraints

  • 1 ≤ s.length ≤ 4000
  • s consists of only lowercase English letters
  • The string is non-empty
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
23.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen