human
Give me a code to count of substrings having all distinct characters and the other steps required given the following:
Count of substrings having all distinct characters | Function to count total number of valid substrings.
Stores the count of substrings.
Stores the frequency of characters.
Initialised both pointers to beginning of the string.
If all characters in substring from index i to j are distinct.
Increment count of j - th character.
Add all substring ending at j and starting at any index between i and j to the answer.
Increment 2 nd pointer.
If some characters are repeated or j pointer has reached to end.
Decrement count of j - th character.
Increment first pointer.
Return the final count of substrings.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to count of substrings having all distinct characters and the other steps required.
 def countSub ( Str ) :
	n = len ( Str )
	ans = 0
	cnt = 26 * [ 0 ]
	i , j = 0 , 0
	while ( i < n ) :
		if ( j < n and ( cnt [ ord ( Str [ j ] ) - ord ( ' a ' ) ] == 0 ) ) :
			cnt [ ord ( Str [ j ] ) - ord ( ' a ' ) ] += 1
			ans += ( j - i + 1 )
			j += 1
		else :
			cnt [ ord ( Str [ i ] ) - ord ( ' a ' ) ] -= 1
			i += 1
	return ans
Str = " gffg "
print ( countSub ( Str ) )