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