Maximum number of given operations to remove the entire string | Java 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
import java . util . * ; class GFG { static 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 . charAt ( i ) ; String s2 = " " ; if ( i + d . length ( ) < s . length ( ) ) { s2 = s . substring ( i + 1 , d . length ( ) + ( i + 1 ) ) ; } if ( s2 . equals ( d ) ) { c = 1 + find ( s . substring ( i + 1 ) ) ; break ; } } return c ; } public static void main ( String [ ] args ) { String s = " abababab " ; System . out . print ( find ( s ) ) ; } }