-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1255.cpp
More file actions
28 lines (28 loc) · 970 Bytes
/
1255.cpp
File metadata and controls
28 lines (28 loc) · 970 Bytes
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
class Solution {
public:
int maxScore = 0;
void backtracking(vector<string>& words, int currentIdx, int currentScore, vector<int>& count, vector<int>& score) {
int n = words.size();
for (int i = 0; i < 26; ++i) {
if (count[i] < 0) return;
}
maxScore = max(maxScore, currentScore);
for (int i = currentIdx; i < n; ++i) {
int accum = 0;
for (auto& c : words[i]) {
count[c - 'a']--;
accum += score[c - 'a'];
}
backtracking(words, i + 1, currentScore + accum, count, score);
for (auto& c : words[i]) {
count[c - 'a']++;
}
}
}
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
vector<int> count(26, 0);
for (auto& c : letters) count[c - 'a']++;
backtracking(words, 0, 0, count, score);
return maxScore;
}
};