human
What is a code to count of distinct groups of strings formed after performing equivalent operation and the other steps required given the following:
Count of distinct groups of strings formed after performing equivalent operation | Function to perform the find operation to find the parent of a disjoint set.
Function to perform union operation of disjoint set union.
Find the parent of node a and b.
Update the rank.
Function to find the number of distinct strings after performing the given operations.
Stores the parent elements of the sets.
Stores the rank of the sets.
Update parent [ i ] to i.
Stores the total characters traversed through the strings.
Stores the current characters traversed through a string.
Update current [ i ] to false.
Update current [ ch - ' a ' ] to true.
Check if current [ j ] is true.
Update total [ j ] to true.
Add arr [ i ] [ 0 ] - ' a ' and j elements to same set.
Stores the count of distinct strings.
Check total [ i ] is true and parent of i is i only.
Increment the value of distCount by 1.
Print the value of distCount.
Driver Code"
Solution in Python:
assistant
#Here is code to count of distinct groups of strings formed after performing equivalent operation and the other steps required.
 def Find ( parent , a ) :
	if parent [ a ] == a :
		parent [ a ] = a
		return parent [ a ]
	else :
		parent [ a ] = Find ( parent , parent [ a ] )
		return parent [ a ]
def Union ( parent , rank , a , b ) :
	a = Find ( parent , a )
	b = Find ( parent , b )
	if ( rank [ a ] == rank [ b ] ) :
		rank [ a ] += 1
	if ( rank [ a ] > rank [ b ] ) :
		parent [ b ] = a
	else :
		parent [ a ] = b
def numOfDistinctStrings ( arr , N ) :
	parent = [ 0 for _ in range ( 27 ) ]
	rank = [ 0 for _ in range ( 27 ) ]
	for j in range ( 0 , 27 ) :
		parent [ j ] = j
	total = [ False for _ in range ( 26 ) ]
	current = [ False for _ in range ( 26 ) ]
	for i in range ( 0 , N ) :
		for j in range ( 0 , 26 ) :
			current [ j ] = False
		for ch in arr [ i ] :
			current [ ord ( ch ) - ord ( ' a ' ) ] = True
		for j in range ( 0 , 26 ) :
			if ( current [ j ] ) :
				total [ j ] = True
				Union ( parent , rank , ord ( arr [ i ] [ 0 ] ) - ord ( ' a ' ) , j )
	distCount = 0
	for i in range ( 0 , 26 ) :
		if ( total [ i ] and Find ( parent , i ) == i ) :
			distCount += 1
	print ( distCount )
if __name__ == " _ _ main _ _ " :
	arr = [ " a " , " ab " , " b " , " d " ]
	N = len ( arr )
	numOfDistinctStrings ( arr , N )