1160. 拼写单词
为保证权益,题目请参考 1160. 拼写单词(From LeetCode).
解决方案1
CPP
C++
#include <iostream>
#include <vector>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
int countCharacters(vector<string> &words, string chars)
{
int char_num[26] = {0};
fill(char_num, char_num + 26, 0);
int word_num[26] = {0};
for(char ch:chars)
{
char_num[ch-'a']++;
}
int ans = 0;
for (string word : words)
{
fill(word_num, word_num + 26, 0);
for(char ch:word)
{
word_num[ch-'a']++;
}
bool flag = true;
for(char ch:word)
{
if(word_num[ch-'a'] > char_num[ch-'a'])
{
flag = false;
break;
}
}
if(flag)
{
ans += word.length();
}
}
return ans;
}
};
int main()
{
return 0;
}
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
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