Maximum Deletions on a String

Problem Statement:

This problem is given that the input is a string s that contains only lowercase English alphabets.

Remove all characters in the strings, including blank spaces.

• In which case, substr(s, 0, i) = substr(s, i, s.length - i) if substr(s, 0, i) = substr(s, i, s.length - i) or 1 <= i <= s.length / 2.
• To be more specific, if s = "ababc", there is a possibility where you could subtract first two letters of s to get "abc", because the first and second letters of s and the subsequent two characters of s are all "ab".

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

Java Approach Using Dynamic programming

Output:

Code Explanation:

• The deleteString method initializes the dynamic programming array dp and calls the helper method with the input string and starting index.
• The helper method recursively computes the maximum number of deletions starting from the given index. It iterates through the string, checking for palindromic substrings. If a palindrome is found, it recursively calls itself with the next index. The base case is when the index reaches the end of the string.
• If no palindromic substring is found, it returns 1, indicating that no deletion is possible from this index onwards.

Time Complexity:

• This convinces us that the helper method has a time complexity of O(n^2), where n is the length of the input string. Deleting the smallest item from the list is an O(n) operation; therefore, deleting all of them is O(n^2), but the visiting of the helper is only once.

Space Complexity:

• The complexity of space is O(n), where n is the quantity of a given string of length. We chose a two-dimensional matrix dp as the size of its dimensionality is n for convenience in saving the intermediate results of dynamic programming. Besides, the stack can even grow theoretically up to O(n) in the worst case, which increases the whole space complexity to O(n).

Java Approach Using HashMap

Output:

Code Explanation:

• This code aims to find the maximum number of characters that can be deleted from a string such that the remaining string becomes a palindrome. It first counts the frequency of each character using a HashMap. If all characters are the same, it returns the count of any character as the result.
• Otherwise, it recursively tries all possible prefix lengths to split the string into a potential palindrome prefix and a remaining suffix. For each valid prefix length, it checks if the prefix is a palindrome. If it is, it recursively calls the function for the remaining suffix and updates the maximum number of deletions accordingly.

Time Complexity:

• The running time of the deleteString function has an order of O(N^2), where N is the length of the input string.
• The time complexity of the Do function (worst-case) is O(N^2). The function iterates through all the prefix lengths, resulting in this worst-case.
• do_check(O(N)) takes time that is proportional to the length of N's substrings.

Space Complexity:

• The time complexity of the program is O(N) due to the use of the HashMap map1, the memoization array dp, and N is the input length.

Java Approach Using Queue

Output:

Code Explanation:

This code aims to find the maximum number of operations required to delete characters from a given string while maintaining the property that each substring within the original string, of length at least 1, should be a palindrome.

• The Deq class manages two queues to represent substrings from two different positions within the string. It tracks the number of matching characters between the substrings.
• In the Solution class, it iterates through all possible substrings and checks if the next substring can be made into a palindrome by deleting characters. It stores this information in a 2D array samenext.

Then, it utilizes dynamic programming to calculate the maximum number of operations required to delete characters.

Time Complexity:

• Construction of same next matrix: O(n^2) as it iterates over all substrings.
• Dynamic programming: O(n^2) due to nested loops.

Space Complexity:

• Same next matrix: O(n^2) to store Boolean values.
• Deq class: O(n) for storing characters.
• Dynamic programming array: O(n) for storing intermediate results.