diff --git a/1255. Maximum Score Words Formed by Letters b/1255. Maximum Score Words Formed by Letters new file mode 100644 index 0000000..23519fe --- /dev/null +++ b/1255. Maximum Score Words Formed by Letters @@ -0,0 +1,45 @@ +class Solution { +public: + int n; + array freq; + bool f(array& freq, int& point, string& w, vector& score){ + for(char c: w){// time O(|w|) + int idx=c-'a'; + freq[idx]--; + point+=score[idx]; + if ( freq[idx]<0) return 0; + } + return 1; + } + int ans=0; + void dfs(int i, array& left, int& point, vector& words, vector& score) + { + if (i==n) return ; + array left0=left; + int point0=point; + if (f(left, point, words[i], score)){ + ans=max(ans, point); + dfs(i+1, left, point, words, score); + } + dfs(i+1, left0, point0, words, score);//backtracking + } + int maxScoreWords(vector& words, vector& letters, vector& score) + { + n=words.size(); + freq.fill(0); + for(char c: letters) + freq[c-'a']++; + + int point=0; + dfs(0, freq, point, words, score); + return ans; + } +}; + + +auto init = []() { + ios::sync_with_stdio(0); + cin.tie(0); + cout.tie(0); + return 'c'; +}();