human
Count substrings with different first and last characters | Function to count the substrings having different first & last character.
Stores frequency of each char.
Loop to store frequency of the characters in a Map.
To store final result.
Traversal of string.
Store answer for every iteration.
Map traversal.
Compare current char.
Print the final count.
Given string.
Length of the string.
Function Call"
How can the above be solved in Python?
assistant
#Here is code to count substrings with different first and last characters and the other steps required.
 def countSubstring ( s , n ) :
	m = { }
	for i in range ( n ) :
		if s [ i ] in m :
			m [ s [ i ] ] += 1
		else :
			m [ s [ i ] ] = 1
	ans = 0
	for i in range ( n ) :
		cnt = 0
		if s [ i ] in m :
			m [ s [ i ] ] -= 1
		else :
			m [ s [ i ] ] = - 1
		for value in m :
			if ( value == s [ i ] ) :
				continue
			else :
				cnt += m [ value ]
		ans += cnt
	print ( ans )
S = " abcab "
N = 5
countSubstring ( S , N )