Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2430.html

2430. Maximum Deletions on a String

  • Difficulty: Hard.
  • Related Topics: .
  • Similar Questions: Shortest Palindrome, Longest Happy Prefix, Remove All Occurrences of a Substring.

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 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.

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 the **maximum number of operations needed to delete all of **s.

  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 <= 4000

  • s consists only of lowercase English letters.

Solution (Java, C++, Python)

  • 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] = 1 + lcp[i + 1][j + 1];
                    }
                }
            }
            int[] dp = new int[n];
            Arrays.fill(dp, 1);
            for (int i = n - 1; i >= 0; --i) {
                for (int j = 1; j <= (n - i) / 2; ++j) {
                    if (lcp[i][i + j] >= j) {
                        dp[i] = Math.max(dp[i], dp[i + j] + 1);
                    }
                }
            }
            return dp[0];
        }
    }
    
  • class Solution {
    public:
        int deleteString(string s) {
            int n = s.size();
            int lcp[n + 1][n + 1];
            memset(lcp, 0, sizeof lcp);
            for (int i = n - 1; i >= 0; --i) {
                for (int j = n - 1; j >= 0; --j) {
                    if (s[i] == s[j]) {
                        lcp[i][j] = 1 + lcp[i + 1][j + 1];
                    }
                }
            }
            int dp[n];
            for (int i = n - 1; i >= 0; --i) {
                dp[i] = 1;
                for (int j = 1; j <= (n - i) / 2; ++j) {
                    if (lcp[i][i + j] >= j) {
                        dp[i] = max(dp[i], dp[i + j] + 1);
                    }
                }
            }
            return dp[0];
        }
    };
    
  • class Solution:
        def deleteString(self, s: str) -> int:
            @cache
            def dfs(i):
                if i == n:
                    return 0
                ans = 1
                m = (n - i) >> 1
                for j in range(1, m + 1):
                    if s[i : i + j] == s[i + j : i + j + j]:
                        ans = max(ans, 1 + dfs(i + j))
                return ans
    
            n = len(s)
            return dfs(0)
    
    
  • func 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] = 1 + lcp[i+1][j+1]
    			}
    		}
    	}
    	dp := make([]int, n)
    	for i := n - 1; i >= 0; i-- {
    		dp[i] = 1
    		for j := 1; j <= (n-i)/2; j++ {
    			if lcp[i][i+j] >= j {
    				dp[i] = max(dp[i], dp[i+j]+1)
    			}
    		}
    	}
    	return dp[0]
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
  • function deleteString(s: string): number {
        const n = s.length;
        const f: number[] = new Array(n).fill(1);
        for (let i = n - 1; i >= 0; --i) {
            for (let j = 1; j <= (n - i) >> 1; ++j) {
                if (s.slice(i, i + j) === s.slice(i + j, i + j + j)) {
                    f[i] = Math.max(f[i], f[i + j] + 1);
                }
            }
        }
        return f[0];
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions