-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1684_countConsistentStrings.cc
More file actions
52 lines (47 loc) · 1022 Bytes
/
Problem_1684_countConsistentStrings.cc
File metadata and controls
52 lines (47 loc) · 1022 Bytes
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
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int countConsistentStrings(string allowed, vector<string> &words)
{
int mask = 0;
int ans = 0;
for (char &c : allowed)
{
mask |= 1 << (c - 'a');
}
for (string &w : words)
{
int seen = 0;
for (char &c : w)
{
seen |= 1 << (c - 'a');
}
if ((mask | seen) == mask)
{
ans++;
}
}
return ans;
}
};
void testCountConsistentStrings()
{
Solution s;
vector<string> n1 = {"ad", "bd", "aaab", "baa", "badab"};
vector<string> n2 = {"a", "b", "c", "ab", "ac", "bc", "abc"};
vector<string> n3 = {"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"};
EXPECT_EQ_INT(2, s.countConsistentStrings("ab", n1));
EXPECT_EQ_INT(7, s.countConsistentStrings("abc", n2));
EXPECT_EQ_INT(4, s.countConsistentStrings("cad", n3));
EXPECT_SUMMARY;
}
int main()
{
testCountConsistentStrings();
return 0;
}