-
Notifications
You must be signed in to change notification settings - Fork 85
/
1255 leetcode.cpp
55 lines (55 loc) · 1.84 KB
/
1255 leetcode.cpp
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
48
49
50
51
52
53
54
55
class Solution {
public:
unordered_map<int, unordered_map<string, int>>dp;
int helper(vector<string>& words, int curr, vector<char> letters, vector<int>& score, string memory){
if(curr<0)return 0;
if(dp[curr][memory]!=0)return dp[curr][memory];
unordered_map<char, int>curr_dict, curr_word, remaining;
vector<char>lettersr;
for(int i=0;i<letters.size();i++){
curr_dict[letters[i]]++;
}
for(int i=0;i<words[curr].size();i++){
curr_word[words[curr][i]]++;
}
remaining=curr_dict;
int flag=1;
for(auto temp:curr_word){
if(curr_dict[temp.first]<temp.second){
flag=0;
break;
}
remaining[temp.first]-=temp.second;
}
string a="", b="";
for(int i=0;i<letters.size();i++){
a+=letters[i];
}
for(int i=0;i<remaining.size();i++){
b+=remaining[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if(flag==0){
return helper(words, curr-1, letters, score, a);
}
int curr_score=0;
for(int i=0;i<words[curr].size();i++){
curr_score+=score[words[curr][i]-'a'];
}
for(auto temp:remaining){
for(int i=0;i<temp.second;i++)
lettersr.emplace_back(temp.first);
}
int ans2=helper(words, curr-1, lettersr, score, b)+curr_score;
return max(helper(words, curr-1, letters, score, a), ans2);
}
int maxScoreWords(vector<string>& words, vector<char>& letters, vector<int>& score) {
string a="";
for(int i=0;i<letters.size();i++){
a+=letters[i];
}
sort(a.begin(), a.end());
return helper(words, words.size()-1, letters, score,a);
}
};