human
What is a code to count of distinct permutation of a String obtained by swapping only unequal characters and the other steps required given the following:
Count of distinct permutation of a String obtained by swapping only unequal characters | Function to calculate total number of valid permutations.
Creating count which is equal to the Total number of characters present and ans that will store the number of unique permutations.
Storing frequency of each character present in the string.
Adding count of characters by excluding characters equal to current char.
Reduce the frequency of the current character and count by 1 , so that it cannot interfere with the calculations of the same elements present to the right of it ..
Return ans + 1 ( Because the given string is also a unique permutation ).
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to count of distinct permutation of a String obtained by swapping only unequal characters and the other steps required.
 def validPermutations ( str ) :
	m = { }
	count = len ( str )
	ans = 0
	for i in range ( len ( str ) ) :
		if ( str [ i ] in m ) :
			m [ str [ i ] ] += 1
		else :
			m [ str [ i ] ] = 1
	for i in range ( len ( str ) ) :
		ans += count - m [ str [ i ] ]
		m [ str [ i ] ] -= 1
		count -= 1
	return ans + 1
if __name__ == ' _ _ main _ _ ' :
	str = " sstt "
	print ( validPermutations ( str ) )