Check if a string can be split into substrings starting with N followed by N characters | Function to check if the given can be split into desired substrings ; Length of the string ; Traverse the string ; Extract the digit ; Check if the extracted number does not exceed the remaining length ; Check for the remaining string ; If generating desired substrings is not possible ; Driver Code
def helper ( s , pos ) : NEW_LINE INDENT size = len ( s ) NEW_LINE if ( pos >= size ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( s [ pos ] . isdigit ( ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( pos , size ) : NEW_LINE INDENT num = num * 10 + ord ( s [ pos ] ) - 48 NEW_LINE if ( i + 1 + num > size ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( helper ( s , i + 1 + num ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s = "123abc4db1c " ; NEW_LINE if ( helper ( s , 0 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT