human
Count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) | Set 2 | Function to count the numbers.
Array that stores the digits from left to right.
Array that stores the digits from right to left.
Initially both array store zero.
Fill the table for count1 array.
Update the count of current character.
Fill the table for count2 array.
Update the count of cuuent character.
Variable that stores the count of the numbers.
Traverse Input string and get the count of digits from count1 and count2 array such that difference b / w digit is 1 & store it int c1 & c2 . And store it in variable c1 and c2.
Update the ans.
Return the final count.
Given String.
Function call"
Solution in Python:
assistant
#Here is code to count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) and the other steps required.
 def countStableNum ( Str , N ) :
	count1 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ]
	count2 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ]
	for i in range ( N ) :
		for j in range ( 10 ) :
			count1 [ i ] [ j ] , count2 [ i ] [ j ] = 0 , 0
	for i in range ( N ) :
		if ( i != 0 ) :
			for j in range ( 10 ) :
				count1 [ i ] [ j ] = ( count1 [ i ] [ j ] + count1 [ i - 1 ] [ j ] )
		count1 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] += 1
	for i in range ( N - 1 , - 1 , - 1 ) :
		if ( i != N - 1 ) :
			for j in range ( 10 ) :
				count2 [ i ] [ j ] += count2 [ i + 1 ] [ j ]
		count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] = count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] + 1
	ans = 0
	for i in range ( 1 , N - 1 ) :
		if ( Str [ i ] == '9' ) :
			continue
		c1 = count1 [ i - 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) ]
		c2 = count2 [ i + 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) + 1 ]
		if ( c2 == 0 ) :
			continue
		ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) // 2 ) ) ) )
	return ans
Str = "224353"
N = len ( Str )
print ( countStableNum ( Str , N ) )