Maximum Deletions on a String
HardUpdated: Aug 2, 2025
Practice on:
Problem
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
iletters ofsif the firstiletters ofsare equal to the followingiletters ins, for anyiin the range1 <= i <= s.length / 2.
For example, if s = "ababc", then in one operation, you could delete the first two letters of s to get "abc", since the first two letters of s
and the following two letters of s are both equal to "ab".
Return _themaximum number of operations needed to delete all of _s.
Examples
Example 1
Input: s = "abcabcdabc"
Output: 2
Explanation:
- Delete the first 3 letters ("abc") since the next 3 letters are equal. Now, s = "abcdabc".
- Delete all the letters.
We used 2 operations so return 2. It can be proven that 2 is the maximum number of operations needed.
Note that in the second operation we cannot delete "abc" again because the next occurrence of "abc" does not happen in the next 3 letters.
Example 2
Input: s = "aaabaab"
Output: 4
Explanation:
- Delete the first letter ("a") since the next letter is equal. Now, s = "aabaab".
- Delete the first 3 letters ("aab") since the next 3 letters are equal. Now, s = "aab".
- Delete the first letter ("a") since the next letter is equal. Now, s = "ab".
- Delete all the letters.
We used 4 operations so return 4. It can be proven that 4 is the maximum number of operations needed.
Example 3
Input: s = "aaaaa"
Output: 5
Explanation: In each operation, we can delete the first letter of s.
Constraints
1 <= s.length <= 4000sconsists only of lowercase English letters.
Solution
Method 1 – Dynamic Programming with Rolling Hash
Intuition
The key idea is to use dynamic programming to find the maximum number of deletions. For each substring, we check if the first i characters are equal to the next i characters, which allows us to delete the prefix. Rolling hash helps us compare substrings efficiently, and we use a table to store the longest common prefix (lcp[i][j]) between substrings starting at i and j.
Approach
- Use a DP array where
dp[i]is the maximum number of operations to deletes[i:]. - For each position
i, try all possible lengthsj(1to(n-i)//2) such thats[i:i+j] == s[i+j:i+2j]. - Use a longest common prefix table (
lcp[i][j]) to compare substrings inO(1)time. - Update
dp[i]asmax(dp[i], 1 + dp[i+j])for all validj. - The answer is
dp[0].
Code
C++
class Solution {
public:
int deleteString(string s) {
int n = s.size();
vector<int> dp(n+1, 0);
vector<vector<int>> lcp(n+1, vector<int>(n+1, 0));
for (int i = n-1; i >= 0; --i) {
for (int j = n-1; j >= 0; --j) {
if (s[i] == s[j]) lcp[i][j] = lcp[i+1][j+1] + 1;
}
}
for (int i = n-1; i >= 0; --i) {
dp[i] = 1;
for (int j = 1; i + 2*j <= n; ++j) {
if (lcp[i][i+j] >= j) dp[i] = max(dp[i], 1 + dp[i+j]);
}
}
return dp[0];
}
};
Go
def deleteString(s string) int {
n := len(s)
lcp := make([][]int, n+1)
for i := range lcp {
lcp[i] = make([]int, n+1)
}
for i := n-1; i >= 0; i-- {
for j := n-1; j >= 0; j-- {
if s[i] == s[j] {
lcp[i][j] = lcp[i+1][j+1] + 1
}
}
}
dp := make([]int, n+1)
for i := n-1; i >= 0; i-- {
dp[i] = 1
for j := 1; i+2*j <= n; j++ {
if lcp[i][i+j] >= j {
if dp[i] < 1+dp[i+j] {
dp[i] = 1+dp[i+j]
}
}
}
}
return dp[0]
}
Java
class Solution {
public int deleteString(String s) {
int n = s.length();
int[][] lcp = new int[n+1][n+1];
for (int i = n-1; i >= 0; i--) {
for (int j = n-1; j >= 0; j--) {
if (s.charAt(i) == s.charAt(j)) {
lcp[i][j] = lcp[i+1][j+1] + 1;
}
}
}
int[] dp = new int[n+1];
for (int i = n-1; i >= 0; i--) {
dp[i] = 1;
for (int j = 1; i+2*j <= n; j++) {
if (lcp[i][i+j] >= j) {
dp[i] = Math.max(dp[i], 1 + dp[i+j]);
}
}
}
return dp[0];
}
}
Kotlin
class Solution {
fun deleteString(s: String): Int {
val n = s.length
val lcp = Array(n+1) { IntArray(n+1) }
for (i in n-1 downTo 0) {
for (j in n-1 downTo 0) {
if (s[i] == s[j]) lcp[i][j] = lcp[i+1][j+1] + 1
}
}
val dp = IntArray(n+1)
for (i in n-1 downTo 0) {
dp[i] = 1
for (j in 1..((n-i)/2)) {
if (lcp[i][i+j] >= j) dp[i] = maxOf(dp[i], 1 + dp[i+j])
}
}
return dp[0]
}
}
Python
def deleteString(s: str) -> int:
n = len(s)
lcp = [[0]*(n+1) for _ in range(n+1)]
for i in range(n-1, -1, -1):
for j in range(n-1, -1, -1):
if s[i] == s[j]:
lcp[i][j] = lcp[i+1][j+1] + 1
dp = [0]*(n+1)
for i in range(n-1, -1, -1):
dp[i] = 1
for j in range(1, (n-i)//2+1):
if lcp[i][i+j] >= j:
dp[i] = max(dp[i], 1 + dp[i+j])
return dp[0]
Rust
impl Solution {
pub fn delete_string(s: String) -> i32 {
let n = s.len();
let s = s.as_bytes();
let mut lcp = vec![vec![0; n+1]; n+1];
for i in (0..n).rev() {
for j in (0..n).rev() {
if s[i] == s[j] {
lcp[i][j] = lcp[i+1][j+1] + 1;
}
}
}
let mut dp = vec![0; n+1];
for i in (0..n).rev() {
dp[i] = 1;
for j in 1..=((n-i)/2) {
if lcp[i][i+j] >= j {
dp[i] = dp[i].max(1 + dp[i+j]);
}
}
}
dp[0]
}
}
TypeScript
class Solution {
deleteString(s: string): number {
const n = s.length;
const lcp: number[][] = Array.from({length: n+1}, () => Array(n+1).fill(0));
for (let i = n-1; i >= 0; i--) {
for (let j = n-1; j >= 0; j--) {
if (s[i] === s[j]) lcp[i][j] = lcp[i+1][j+1] + 1;
}
}
const dp: number[] = Array(n+1).fill(0);
for (let i = n-1; i >= 0; i--) {
dp[i] = 1;
for (let j = 1; i+2*j <= n; j++) {
if (lcp[i][i+j] >= j) dp[i] = Math.max(dp[i], 1 + dp[i+j]);
}
}
return dp[0];
}
}
Complexity
- ⏰ Time complexity:
O(n^2), because for each position we check all possible substring lengths and fill the lcp table. - 🧺 Space complexity:
O(n^2), due to the lcp table and dp array.