leetcode 1160. スペル単語(C++)

1616 ワード

「語彙」(文字列配列)wordsと「アルファベット」(文字列)charsをあげます.charsの「アルファベット」(文字)でwordsの「単語」(文字列)をつづることができれば、私たちはあなたがこの単語をマスターしたと思っています.
注意:charsのアルファベットは、スペルのたびに1回しか使用できません.
語彙表wordsで習得したすべての単語の長さの和を返します.
 
例1:
  :words = ["cat","bt","hat","tree"], chars = "atach"
  :6
  : 
        "cat"   "hat",      3 + 3 = 6。

例2:
  :words = ["hello","world","leetcode"], chars = "welldonehoneyr"
  :10
  :
        "hello"   "world",      5 + 5 = 10。

 
ヒント:
  • 1 <= words.length <= 1000
  • 1 <= words[i].length, chars.length <= 100
  • すべての文字列には、小文字の英字
  • のみが含まれています.
    C++
    class Solution {
    public:
        int countCharacters(vector& words, string chars) 
        {
            int m=words.size();
            int n=chars.size();
            int res=0;
            map tmp;
            for(int i=0;i vec;
                for(auto c:words[i])
                {
                    vec[c]++;
                }
                int flag=1;
                for(auto it:vec)
                {
                    if(it.second>tmp[it.first])
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag)
                {
                    res+=words[i].length();
                }
            }
            return res;
        }
    };