Skip to content

Commit 77b0009

Browse files
authored
Create 1255. Maximum Score Words Formed by Letters
1 parent b78a9a5 commit 77b0009

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
int n;
4+
array<char, 26> freq;
5+
bool f(array<char, 26>& freq, int& point, string& w, vector<int>& score){
6+
for(char c: w){// time O(|w|)
7+
int idx=c-'a';
8+
freq[idx]--;
9+
point+=score[idx];
10+
if ( freq[idx]<0) return 0;
11+
}
12+
return 1;
13+
}
14+
int ans=0;
15+
void dfs(int i, array<char, 26>& left, int& point, vector<string>& words, vector<int>& score)
16+
{
17+
if (i==n) return ;
18+
array<char, 26> left0=left;
19+
int point0=point;
20+
if (f(left, point, words[i], score)){
21+
ans=max(ans, point);
22+
dfs(i+1, left, point, words, score);
23+
}
24+
dfs(i+1, left0, point0, words, score);//backtracking
25+
}
26+
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score)
27+
{
28+
n=words.size();
29+
freq.fill(0);
30+
for(char c: letters)
31+
freq[c-'a']++;
32+
33+
int point=0;
34+
dfs(0, freq, point, words, score);
35+
return ans;
36+
}
37+
};
38+
39+
40+
auto init = []() {
41+
ios::sync_with_stdio(0);
42+
cin.tie(0);
43+
cout.tie(0);
44+
return 'c';
45+
}();

0 commit comments

Comments
 (0)