leetcode 1160. スペル単語(C++)
1616 ワード
「語彙」(文字列配列)
注意:
語彙表
例1:
例2:
ヒント: すべての文字列には、小文字の英字 のみが含まれています.
C++
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;
}
};