human
Can you provide a code to count of substrings formed using a given set of characters only and the other steps required given the following:
Count of substrings formed using a given set of characters only | Function to find the number of substrings that can be formed using given characters.
Boolean array for storing the available characters.
Mark indices of all available characters as 1.
Initialize lastPos as - 1.
Initialize ans with the total no of possible substrings.
Traverse the string from left to right.
If the current character is not present in B.
Subtract the total possible substrings.
Update the value of lastpos to current index.
Print the final answer.
Given String.
Given character array.
Function call"
How can the above be solved in Python?
assistant
#Here is code to count of substrings formed using a given set of characters only and the other steps required.
 def numberofsubstrings ( str , k , charArray ) :
	N = len ( str )
	available = [ 0 ] * 26
	for i in range ( 0 , k ) :
		available [ ord ( charArray [ i ] ) - ord ( ' a ' ) ] = 1
	lastPos = - 1
	ans = ( N * ( N + 1 ) ) / 2
	for i in range ( 0 , N ) :
		if ( available [ ord ( str [ i ] ) - ord ( ' a ' ) ] == 0 ) :
			ans -= ( ( i - lastPos ) * ( N - i ) )
			lastPos = i
	print ( int ( ans ) )
str = " abcb "
k = 2
charArray = [ ' a ' , ' b ' ]
numberofsubstrings ( str , k , charArray )