Check if a string can become empty by recursively deleting a given sub | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBecomeEmpty ( string str , string sub_str ) { while ( str . size ( ) > 0 ) { int idx = str . find ( sub_str ) ; if ( idx == -1 ) break ; str . erase ( idx , sub_str . size ( ) ) ; } return ( str . size ( ) == 0 ) ; } int main ( ) { string str = " GEEGEEKSKS " , sub_str = " GEEKS " ; if ( canBecomeEmpty ( str , sub_str ) ) cout << " Yes " else cout << " No " return 0 ; }