Expand the string according to the given conditions | Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
def expandString ( strin ) : NEW_LINE INDENT temp = " " NEW_LINE j = 0 NEW_LINE i = 0 NEW_LINE while ( i < len ( strin ) ) : NEW_LINE INDENT if ( strin [ i ] >= "0" ) : NEW_LINE INDENT num = ord ( strin [ i ] ) - ord ( "0" ) NEW_LINE if ( strin [ i + 1 ] == ' ( ' ) : NEW_LINE INDENT j = i + 1 NEW_LINE while ( strin [ j ] != ' ) ' ) : NEW_LINE INDENT if ( ( strin [ j ] >= ' a ' and strin [ j ] <= ' z ' ) or ( strin [ j ] >= ' A ' and strin [ j ] <= ' Z ' ) ) : NEW_LINE INDENT temp += strin [ j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT for k in range ( 1 , num + 1 ) : NEW_LINE INDENT print ( temp , end = " " ) NEW_LINE DEDENT num = 0 NEW_LINE temp = " " NEW_LINE if ( j < len ( strin ) ) : NEW_LINE INDENT i = j NEW_LINE DEDENT DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT strin = "3 ( ab ) 4 ( cd ) " NEW_LINE expandString ( strin ) NEW_LINE