Maximum Score Words Formed by Letters in C++

C++Server Side ProgrammingProgramming

Suppose we have a list of words, a list of single letters and score for every character. We have to find the maximum score of any valid set of words formed by using the given letters.

We may not use all characters in letters and each letter can only be used once. Score of letters 'a', 'b', 'c', ... ,'z' is given by score[0], score[1], ... , score[25] respectively.

So, if the input is like words = ["god", "good", "toc", "cat"], letters = [a,g,o,o,d,d,d,c,t,t] and score = [5,0,8,3,0,0,6,0,0,0,0,0,0,0,3,0,0,0,0,2,0,0,0,0,0,0], then the output will be 30, here good and cat are making maximum score.

To solve this, we will follow these steps −

  • Define one 2D array dp

  • Define a function calc(), this will take s, one map m, an array sc,

  • ans := 0

  • for initialize i := 0, when i < size of s, update (increase i by 1), do −

    • x := s[i]

    • if m[x] <= 0, then −

      • return 0

    • (decrease m[x] by 1)

    • ans := ans + sc[x - 'a']

  • return ans

  • Define a function solve(), this will take i, status, an array of pairs v, one map m, an array s,

  • if i is same as -1, then −

    • return 0

  • x := .second value of v[i]

  • ans := 0

  • if status is same as 1, then −

    • ans := calc(x, m, s)

  • if ans > 0 and status is same as 1, then −

    • for initialize j := 0, when j < size of x, update (increase j by 1), do −

      • (decrease m[x[j]] by 1)

  • return ans + maximum of solve(i - 1, 0, v, m, s) and solve(i - 1, 1, v, m, s)

  • From the main method, do the following −

  • ans := 0

  • Define one map m

  • for initialize i := 0, when i < size of l, update (increase i by 1), do −

    • (increase m[l[i]] by 1)

  • Define an array v of pairs

  • for initialize i := 0, when i < size of w, update (increase i by 1), do −

    • x := w[i]

    • flag := calc(x, m, s)

    • if flag is non-zero, then −

      • insert { flag, x } at the end of v

  • sort the array v

    dp := Define one 2D array of size (size of v) x 2 and fill this with -1

    return maximum of solve(size of v, 0, v, m, s) and solve(size of v, 1, v, m, s)

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   vector<vector<int> > dp;
   int calc(string s, map<char, int> m, vector<int>& sc){
      int ans = 0;
      for (int i = 0; i < s.size(); i++) {
         char x = s[i];
         if (m[x] <= 0)
            return 0;
         m[x]--;
         ans += sc[x - 'a'];
      }
      return ans;
   }
   int solve(int i, int status, vector<pair<int, string> > v,
   map<char, int> m, vector<int>& s){
      if (i == -1)
         return 0;
      string x = v[i].second;
      int ans = 0;
      if (status == 1)
         ans = calc(x, m, s);
      if (ans > 0 && status == 1) {
         for (int j = 0; j < x.size(); j++) {
            m[x[j]]--;
         }
      }
      return ans + max(solve(i - 1, 0, v, m, s), solve(i - 1, 1, v, m, s));
   }
   int maxScoreWords(vector<string>& w, vector<char>& l,
   vector<int>& s){
      int ans = 0;
      map<char, int> m;
      for (int i = 0; i < l.size(); i++)
         m[l[i]]++;
      vector<pair<int, string> > v;
      for (int i = 0; i < w.size(); i++) {
         string x = w[i];
         int flag = calc(x, m, s);
         if (flag) {
            v.push_back({ flag, x });
         }
      }
      sort(v.begin(), v.end());
      dp = vector<vector<int> >(v.size(), vector<int>(2, -1));
      return max(solve(v.size() - 1, 0, v, m, s), solve(v.size() -
      1, 1, v, m, s));
   }
};
main(){
   Solution ob;
   vector<string> words = {"god", "good", "toc", "cat"};
   vector<char> letters = {'a','g','o','o','d','d','d','c','t','t'};
   vector<int> score = {5,0,8,3,0,0,6,0,0,0,0,0,0,0,3,0,0,0,0,2,0,0,0,0,0,0};
   cout << (ob.maxScoreWords(words, letters, score));
}

Input

{"god", "good", "toc", "cat"},
{'a','g','o','o','d','d','d','c','t','t'},
{5,0,8,3,0,0,6,0,0,0,0,0,0,0,3,0,0,0,0,2,0,0,0,0,0,0}

Output

30
raja
Published on 04-Jun-2020 11:49:10
Advertisements