Skip to content

Commit 4882a4b

Browse files
Create Maximum Score Words Formed by Letters.cpp
1 parent 4169b14 commit 4882a4b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
4+
vector<int> count(26, 0);
5+
vector<int> lettersCount(26, 0);
6+
for(auto &c : letters){
7+
count[c - 'a']++;
8+
}
9+
int ans = 0;
10+
backtracking(words, score, count, lettersCount, 0, 0, ans);
11+
return ans;
12+
}
13+
void backtracking(vector<string>& words, vector<int>& score, vector<int>& count, vector<int>& lettersCount, int pos, int temp, int &ans){
14+
for(int i = 0; i < 26; i++){
15+
if(lettersCount[i] > count[i]) return;
16+
}
17+
ans = max(ans, temp);
18+
for(int i = pos; i < words.size(); i++){
19+
for(auto& c : words[i]){
20+
lettersCount[c - 'a']++;
21+
temp+=score[c - 'a'];
22+
}
23+
backtracking(words, score, count, lettersCount, i + 1, temp, ans);
24+
for(auto& c : words[i]){
25+
lettersCount[c - 'a']--;
26+
temp-=score[c - 'a'];
27+
}
28+
}
29+
}
30+
};

0 commit comments

Comments
 (0)