human
Maximize cost of forming a set of words using given set of characters | Function to find the maximum cost of any valid set of words formed by using the given letters.
Stores frequency of characters.
Find the maximum cost.
Utility function to find maximum cost of generating any possible subsets of strings.
Base Case.
Stores the current cost.
Stores the cost of by the current word.
Create a copy of the letterCounts array.
Traverse the current word.
Store the current index & check if its frequency is 0 or not.
If true , then update wordScore to - 1.
Otherwise , add the cost of the current index to wordScore.
Decrease its frequency.
If wordScore > 0 , then recursively call for next index.
Find the cost by not including current word.
Return the maximum score.
Given arrays.
Function Call"
Solution in Python:
 Give me a code to maximize cost of forming a set of words using given set of characters and the other steps required?
assistant
#Here is code to maximize cost of forming a set of words using given set of characters and the other steps required.
 def maxScoreWords ( words , letters , score ) :
	letterCounts = [ 0 ] * ( 26 )
	for letter in letters :
		letterCounts [ ord ( letter ) - ord ( ' a ' ) ] += 1
	return helper ( words , 0 , letterCounts , score )
def helper ( words , start , letterCounts , score ) :
	if ( start == len ( words ) ) :
		return 0
	currScore = 0
	wordScore = 1
	nextCounts = letterCounts
	for i in range ( len ( words [ start ] ) ) :
		idx = ord ( words [ start ] [ i ] ) - ord ( ' a ' )
		if ( nextCounts [ idx ] == 0 ) :
			wordScore = - 1
			break
		wordScore += score [ idx ]
		nextCounts [ idx ] -= 1
	if ( wordScore > 0 ) :
		currScore = helper ( words , start + 1 , nextCounts , score ) + wordScore
	currScore = max ( currScore , helper ( words , start + 1 , letterCounts , score ) )
	return currScore
words = [ " dog " , " cat " , " dad " , " good " ]
letters = [ ' a ' , ' a ' , ' c ' , ' d ' , ' d ' , ' d ' , ' g ' , ' o ' , ' o ' ]
score = [ 1 , 0 , 9 , 5 , 0 , 0 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ]
print ( maxScoreWords ( words , letters , score ) )