-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh1255 v1.py
38 lines (27 loc) · 1.23 KB
/
h1255 v1.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
31
32
33
34
35
36
37
38
class Solution:
def maxScoreWords(self, words: List[str], letters: List[str], score: List[int]) -> int:
lettersCounter = Counter(letters)
wordCounters = {w: Counter(w) for w in words}
currentWords = []
self.maxScore = 0
def getAlphNum(c: str) -> int :
return ord(c) - ord('a')
def getScore(wordsToInclude: List[str]) -> int : # -1 if invalid
mergedCounters = Counter()
for i in range(0, len(wordsToInclude)) :
mergedCounters += wordCounters[wordsToInclude[i]]
if any(cnt > lettersCounter.get(let, 0) for let, cnt in mergedCounters.items()) :
return -1
return sum([cnt * score[getAlphNum(let)] for let, cnt in mergedCounters.items()])
def helper() -> None :
if not words :
self.maxScore = max(self.maxScore, getScore(currentWords))
# if still words to add
if words :
nextWord = words.pop()
helper()
currentWords.append(nextWord)
helper()
words.append(currentWords.pop())
helper()
return self.maxScore