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 ; Prlet the result ; Driver Code
function isPossible ( s , K ) { let valid = false ; let firstx = - 1 ; let n = s . length ; for ( let i = 1 ; i <= n / 2 ; ++ i ) { let x = ( s . substr ( 0 , i ) ) ; firstx = x ; let test = x . toString ( ) ; while ( test . length < s . length ) { x -= K ; test += x . toString ( ) ; } if ( test == s ) { valid = true ; break ; } } document . write ( ( valid == true ) ? " " : " " ) ; } let S = " " ; let K = 2 ; isPossible ( S , K ) ;