Count Words Obtained After Adding a Letter LeetCode solution in C++

1
class Solution {
2
public:
3
int wordCount(vector<string> &startWords, vector<string> &targetWords) {
4
set<vector<int>> hash;
5
for (int i = 0; i < startWords.size(); i++) {
6
vector<int> counter(26, 0);
7
for (char &x : startWords[i]) {
8
counter[x - 'a']++;
9
}
10
hash.insert(counter);
11
}
12

13
int ans = 0;
14

15
for (int i = 0; i < targetWords.size(); i++) {
16
vector<int> counter(26, 0);
17
for (char &x : targetWords[i]) {
18
counter[x - 'a']++;
19
}
20

21
for (int j = 0; j < 26; j++) {
22
if (counter[j] == 1) {
23
counter[j] = 0;
24

25
if (hash.find(counter) != hash.end()) {
26
ans++;
27
break;
28
}
29

30
counter[j] = 1;
31
}
32
}
33
}
34

35
return ans;
36
}
37
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0