-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#1255 Maximum Score Words Formed by Letters.cpp
More file actions
44 lines (39 loc) · 1.42 KB
/
#1255 Maximum Score Words Formed by Letters.cpp
File metadata and controls
44 lines (39 loc) · 1.42 KB
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
39
40
41
42
43
44
class Solution {
public:
int maxScore = 0;
int calculateScore(string &word, unordered_map<char, int>& letterCount, vector<int>& letterScores) {
int currentScore = 0;
for (char ch : word) {
if (letterCount.count(ch)) {
if (letterCount[ch] > 0) {
currentScore += letterScores[ch - 'a'];
letterCount[ch]--;
} else {
return 0;
}
} else {
return 0;
}
}
return currentScore;
}
void findMaxScore(int index, vector<string>& words, vector<int>& letterScores, unordered_map<char, int> letterCount, int currentVal) {
if (index == words.size()) {
maxScore = max(maxScore, currentVal);
return;
}
findMaxScore(index + 1, words, letterScores, letterCount, currentVal);
int wordScore = calculateScore(words[index], letterCount, letterScores);
if (wordScore > 0) {
findMaxScore(index + 1, words, letterScores, letterCount, currentVal + wordScore);
}
}
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& letterScores) {
unordered_map<char, int> letterCount;
for (char ch : letters) {
letterCount[ch]++;
}
findMaxScore(0, words, letterScores, letterCount, 0);
return maxScore;
}
};