Check if a string can become empty by recursively deleting a given sub | Java program to check if a string can be converted to an empty string by deleting given sub - string from any position , any number of times . ; Returns true if str can be made empty by recursively removing sub_str . ; idx : to store starting index of sub - string found in the original string ; Erasing the found sub - string from the original string ; Driver code
class GFG { static boolean canBecomeEmpty ( String str , String sub_str ) { while ( str . length ( ) > 0 ) { int idx = str . indexOf ( sub_str ) ; if ( idx == - 1 ) { break ; } str = str . replaceFirst ( sub_str , " " ) ; } return ( str . length ( ) == 0 ) ; } public static void main ( String [ ] args ) { String str = " GEEGEEKSKS " , sub_str = " GEEKS " ; if ( canBecomeEmpty ( str , sub_str ) ) { System . out . print ( " Yes "); } else { System . out . print ( " No "); } } }