forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaximum-score-words-formed-by-letters.py
30 lines (26 loc) · 1.11 KB
/
maximum-score-words-formed-by-letters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Time: O(n * 2^n)
# Space: O(n)
import collections
class Solution(object):
def maxScoreWords(self, words, letters, score):
"""
:type words: List[str]
:type letters: List[str]
:type score: List[int]
:rtype: int
"""
def backtracking(words, word_scores, word_counts, curr, curr_score, letter_count, result):
result[0] = max(result[0], curr_score)
for i in xrange(curr, len(words)):
if any(letter_count[c] < word_counts[i][c] for c in word_counts[i]):
continue
backtracking(words, word_scores, word_counts, i+1,
curr_score+word_scores[i], letter_count-word_counts[i],
result)
letter_count = collections.Counter(letters)
word_counts = map(collections.Counter, words)
word_scores = [sum(score[ord(c)-ord('a')] for c in words[i])
for i in xrange(len(words))]
result = [0]
backtracking(words, word_scores, word_counts, 0, 0, letter_count, result)
return result[0]