human
Count of substrings from given Ternary strings containing characters at least once | Function to count the number of substrings consists of 0 , 1 , and 2.
Initialize frequency array of size 3.
Stores the resultant count.
Traversing string str.
Update frequency array.
If all the characters are present counting number of substrings possible.
Update number of substrings.
Return the number of substrings.
Driver Code"
Solution in Python:
assistant
#Here is code to count of substrings from given Ternary strings containing characters at least once and the other steps required.
 def countSubstrings ( str ) :
	freq = [ 0 ] * 3
	count = 0
	i = 0
	for j in range ( 0 , len ( str ) ) :
		freq [ ord ( str [ j ] ) - ord ( '0' ) ] += 1
		while ( freq [ 0 ] > 0 and freq [ 1 ] > 0 and freq [ 2 ] > 0 ) :
			i += 1
			freq [ ord ( str [ i ] ) - ord ( '0' ) ] -= 1
		count += i
	return count
str = "00021"
count = countSubstrings ( str )
print ( count )