Check if a numeric string can be split into substrings having difference between consecutive numbers equal to K | Function to check if a numeric string can be split into substrings such that the difference between the consecutive substrings is K ; Stores the size of the string ; Iterate over the range [ 1 , N ] and try each possible starting number ; Convert the number to string ; Build up a sequence starting with the number ; Compare it with the original string s ; Print the result ; Driver Code
def isPossible ( s , K ) : NEW_LINE INDENT valid = False NEW_LINE firstx = - 1 NEW_LINE n = len ( s ) NEW_LINE for i in range ( 1 , n // 2 + 1 , 1 ) : NEW_LINE INDENT x = int ( s [ 0 : i ] ) NEW_LINE firstx = x NEW_LINE test = str ( x ) NEW_LINE while ( len ( test ) < len ( s ) ) : NEW_LINE INDENT x -= K NEW_LINE test += str ( x ) NEW_LINE DEDENT if ( test == s ) : NEW_LINE INDENT valid = True NEW_LINE break NEW_LINE DEDENT DEDENT print ( " Yes " ) if valid == True else print ( " No " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "8642" NEW_LINE K = 2 NEW_LINE isPossible ( S , K ) NEW_LINE DEDENT