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.**Examples:**

Input:str = “abababab”Output:4

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”Output:1

**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:

## C++

`// C++ implementation of the approach` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Function to return the maximum number` `// of given operations required to` `// remove the given string entirely` `int` `find(string s)` `{` ` ` `// If length of the string is zero` ` ` `if` `(s.length() == 0)` ` ` `return` `0;` ` ` `// Single operation can delete the entire string` ` ` `int` `c = 1;` ` ` `// To store the prefix of the string` ` ` `// which is to be deleted` ` ` `string d = ` `""` `;` ` ` `for` `(` `int` `i = 0; i < s.length(); i++) {` ` ` `// Prefix s[0..i]` ` ` `d += s[i];` ` ` `// To store the substring s[i+1...2*i+1]` ` ` `string s2 = s.substr(i + 1, d.length());` ` ` `// If the prefix s[0...i] can be deleted` ` ` `if` `(s2 == d) {` ` ` `// 1 operation to remove the current prefix` ` ` `// and then recursively find the count of` ` ` `// operations for the substring s[i+1...n-1]` ` ` `c = 1 + find(s.substr(i + 1));` ` ` `break` `;` ` ` `}` ` ` `}` ` ` `// Entire string has to be deleted` ` ` `return` `c;` `}` `// Driver code` `int` `main()` `{` ` ` `string s = ` `"abababab"` `;` ` ` `cout << find(s);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach` `# Function to return the maximum number` `# of given operations required to` `# remove the given entirely` `def` `find(s):` ` ` `# If length of the is zero` ` ` `if` `(` `len` `(s) ` `=` `=` `0` `):` ` ` `return` `0` ` ` `# Single operation can delete ` ` ` `# the entire string` ` ` `c ` `=` `1` ` ` `# To store the prefix of the string` ` ` `# which is to be deleted` ` ` `d ` `=` `""` ` ` `for` `i ` `in` `range` `(` `len` `(s)):` ` ` `# Prefix s[0..i]` ` ` `d ` `+` `=` `s[i]` ` ` `# To store the subs[i+1...2*i+1]` ` ` `s2 ` `=` `s[i ` `+` `1` `:i ` `+` `1` `+` `len` `(d)]` ` ` `# If the prefix s[0...i] can be deleted` ` ` `if` `(s2 ` `=` `=` `d):` ` ` `# 1 operation to remove the current prefix` ` ` `# and then recursively find the count of` ` ` `# operations for the subs[i+1...n-1]` ` ` `c ` `=` `1` `+` `find(s[i ` `+` `1` `:])` ` ` `break` ` ` `# Entire has to be deleted` ` ` `return` `c` `# Driver code` `s ` `=` `"abababab"` `print` `(find(s))` `# This code is contributed by Mohit Kumar` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach` `using` `System;` `class` `GFG{` ` ` `// Function to return the maximum number` `// of given operations required to` `// remove the given string entirely` `static` `int` `find(` `string` `s)` `{` ` ` ` ` `// If length of the string is zero` ` ` `if` `(s.Length == 0)` ` ` `return` `0;` ` ` ` ` `// Single operation can delete the entire string` ` ` `int` `c = 1;` ` ` ` ` `// To store the prefix of the string` ` ` `// which is to be deleted` ` ` `string` `d = ` `""` `;` ` ` ` ` `for` `(` `int` `i = 0; i < s.Length; i++) ` ` ` `{` ` ` ` ` `// Prefix s[0..i]` ` ` `d += s[i];` ` ` ` ` `// To store the substring s[i+1...2*i+1]` ` ` `string` `s2 = ` `""` `;` ` ` `if` `(i + d.Length < s.Length)` ` ` `{` ` ` `s2 = s.Substring(i + 1, d.Length);` ` ` `}` ` ` ` ` `// If the prefix s[0...i] can be deleted` ` ` `if` `(s2 == d)` ` ` `{` ` ` `// 1 operation to remove the current prefix` ` ` `// and then recursively find the count of` ` ` `// operations for the substring s[i+1...n-1]` ` ` `c = 1 + find(s.Substring(i + 1));` ` ` `break` `;` ` ` `}` ` ` `}` ` ` ` ` `// Entire string has to be deleted` ` ` `return` `c;` `}` ` ` ` ` `// Driver code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `string` `s = ` `"abababab"` `;` ` ` `Console.Write(find(s));` ` ` `}` `}` `// This code is contributed by rutvik` |

*chevron_right*

*filter_none*

**Output:**

4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count minimum number of fountains to be activated to cover the entire garden
- Find value after N operations to remove N characters of string S with given constraints
- Form N-copy string with add, remove and append operations
- Restore original String from given Encrypted String by the given operations
- Minimum number of given operations required to convert a string to another string
- Minimum given operations required to convert a given binary string to all 1's
- Minimum number of operations required to obtain a given Binary String
- Minimum number of steps needed to remove the substring K from given string
- Remove leading zeros from a Number given as a string
- Maximum value obtained by performing given operations in an Array
- Maximum score possible after performing given operations on an Array
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Find the maximum occurring character after performing the given operations
- Remove characters from the first string which are present in the second string
- Minimum reduce operations to convert a given string into a palindrome
- Final string after performing given operations
- Encrypt the given string with the following operations
- Minimum operations required to make the string satisfy the given condition
- Minimum operations required to convert all characters of a String to a given Character

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.