-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxScoreWordsFormed.java
32 lines (32 loc) · 1.12 KB
/
maxScoreWordsFormed.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
29
30
31
32
class Solution {
public int maxScoreWords(String[] words, char[] letters, int[] score) {
if (words == null || words.length == 0 || letters == null || letters.length == 0 || score == null || score.length == 0) return 0;
int[] count = new int[score.length];
for (char ch : letters) {
count[ch - 'a']++;
}
int res = backtrack(words, count, score, 0);
return res;
}
int backtrack(String[] words, int[] count, int[] score, int index) {
int max = 0;
for (int i = index; i < words.length; i++) {
int res = 0;
boolean isValid = true;
for (char ch : words[i].toCharArray()) {
count[ch - 'a']--;
res += score[ch - 'a'];
if (count[ch - 'a'] < 0) isValid = false;
}
if (isValid) {
res += backtrack(words, count, score, i + 1);
max = Math.max(res, max);
}
for (char ch : words[i].toCharArray()) {
count[ch - 'a']++;
res = 0;
}
}
return max;
}
}