/
MaximumScoreWordsFormedByLetters.java
47 lines (38 loc) · 1.15 KB
/
MaximumScoreWordsFormedByLetters.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://leetcode.com/problems/maximum-score-words-formed-by-letters
class Solution {
public int maxScoreWords(String[] words, char[] letters, int[] score) {
int[] freq = new int[26];
for (char c: letters) {
freq[c - 'a']++;
}
return max(words, freq, score, 0);
}
int max(String[] words, int[] freq, int[] scores, int i) {
if (i >= words.length) {
return 0;
}
int max = 0;
// Compute the score of words[i]
int score = 0;
boolean canForm = true;
for (char c: words[i].toCharArray()) {
if (freq[c - 'a'] == 0) {
score = 0;
canForm = false;
}
freq[c - 'a'] -= 1;
if (canForm)
score += scores[c - 'a'];
}
// Consider this word and get the max
if (score != 0) {
max = score + max(words, freq, scores, i + 1);
}
// Backtrack
for (char c: words[i].toCharArray()) {
freq[c - 'a'] += 1;
}
max = Math.max(max, max(words, freq, scores, i + 1));
return max;
}
}