Given a string str containing lowercase English characters, we can perform the following two operations on the given string:
- Remove the entire string.
- Remove a prefix of the string str[0…i] only if it is equal to the sub-string str[(i + 1)…(2 * i + 1)].
The task is to find the maximum number of operations required to delete the entire string.
Input: str = “abababab”
Operation 1: Delete prefix “ab” and the string becomes “ababab”.
Operation 2: Delete prefix “ab” and the string becomes “abab”.
Operation 3: Delete prefix “ab”, str = “ab”.
Operation 4: Delete the entire string.
Input: s = “abc”
Approach: In order to maximize the number of operations, the prefix that is to be deleted must be of minimum length i.e. starting from a single character and appending the successive characters one by one find the minimum length prefix that satisfies the given condition. One operation will be required to delete this prefix say str[0…i] then recursively call the same function to find the result for the substring str[i+1…2*i+1]. If there is no such prefix that could be deleted then return 1 as the only possible operation is to delete the entire string.
Below is the implementation of the above approach:
- Form N-copy string with add, remove and append operations
- Minimum number of given operations required to convert a string to another string
- Minimum number of operations required to sum to binary string S
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Maximum number of removals of given subsequence from a string
- Find the maximum possible Binary Number from given string
- String with maximum number of unique characters
- Maximum number of characters between any two same character in a string
- Remove characters from the first string which are present in the second string
- Maximum value obtained by performing given operations in an Array
- Encrypt the given string with the following operations
- Final string after performing given operations
- Operations required to make the string empty
- Converting one string to other using append and delete last operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29