-
Notifications
You must be signed in to change notification settings - Fork 481
/
1255.java
28 lines (25 loc) · 826 Bytes
/
1255.java
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 maxScoreWords(String[] words, char[] letters, int[] score) {
cnt = new int[score.length];
n = words.length;
for (char c : letters) {
cnt[c - 97]++;
}
return dfs(0, words, score);
}
private int n;
private int[] cnt;
private int dfs(int i, String[] words, int[] score) {
if (i == n) return 0;
int res = Math.max(0, dfs(i + 1, words, score)), tmp = 0, val = 1;
for (char c : words[i].toCharArray()) {
int t = c - 97;
cnt[t]--;
tmp += score[t];
if (cnt[t] < 0) val = 0;
}
if (val == 1) res = Math.max(res, dfs(i + 1, words, score) + tmp);
for (char c : words[i].toCharArray()) cnt[c - 97]++;
return res;
}
}