forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
34 lines (34 loc) · 922 Bytes
/
Solution.cpp
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
class Solution {
public:
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
int cnt[26]{};
for (char& c : letters) {
cnt[c - 'a']++;
}
int n = words.size();
int ans = 0;
for (int i = 0; i < 1 << n; ++i) {
int cur[26]{};
for (int j = 0; j < n; ++j) {
if (i >> j & 1) {
for (char& c : words[j]) {
cur[c - 'a']++;
}
}
}
bool ok = true;
int t = 0;
for (int j = 0; j < 26; ++j) {
if (cur[j] > cnt[j]) {
ok = false;
break;
}
t += cur[j] * score[j];
}
if (ok && ans < t) {
ans = t;
}
}
return ans;
}
};