Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3f477d

Browse files
authored
_916.cpp (#190)
* Create _916Solution for the leetcode daily question 916. Word Subsets* Rename _916 to _916.cpp
1 parentd421171 commite3f477d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎cpp/_916.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
classSolution {
2+
public:
3+
vector<string>wordSubsets(vector<string>& words1, vector<string>& words2) {
4+
int maxCharFreq[26] = {0};
5+
int tempCharFreq[26];
6+
for (constauto& word : words2) {
7+
memset(tempCharFreq,0,sizeof tempCharFreq);
8+
for (char ch : word) {
9+
tempCharFreq[ch -'a']++;
10+
}
11+
for (int i =0; i <26; ++i) {
12+
maxCharFreq[i] =max(maxCharFreq[i], tempCharFreq[i]);
13+
}
14+
}
15+
vector<string> universalWords;
16+
for (constauto& word : words1) {
17+
memset(tempCharFreq,0,sizeof tempCharFreq);
18+
for (char ch : word) {
19+
tempCharFreq[ch -'a']++;
20+
}
21+
bool isUniversal =true;
22+
for (int i =0; i <26; ++i) {
23+
if (maxCharFreq[i] > tempCharFreq[i]) {
24+
isUniversal =false;
25+
break;
26+
}
27+
}
28+
if (isUniversal) {
29+
universalWords.emplace_back(word);
30+
}
31+
}
32+
return universalWords;
33+
}
34+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp