Skip to content
This repository was archived by the owner on Mar 23, 2025. It is now read-only.

Commit 43040a0

Browse files
committed
feat: Add solution for Problem 916 - Word Subsets
- Implemented a solution to find all universal words from a list that meet the character frequency requirements of another list. - Updated README to include the new problem and solution. - Incremented daily streak to 67 days.
1 parent eb928d4 commit 43040a0

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Welcome to my LeetCode solutions repository! This repository contains my solutions to various LeetCode problems, documenting my journey through algorithmic problem-solving and competitive programming. All solutions are implemented in C++.
44

5-
**Current Daily Streak: 65 days** 🔥
5+
**Current Daily Streak: 67 days** 🔥
66

77
**[Time Travel Tickets](https://support.leetcode.com/hc/en-us/articles/14677342930835-What-Are-Time-Travel-Tickets) Used: 3**
88

@@ -93,6 +93,7 @@ solutions/
9393
| 2025-01-07 | 1408 | [String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/) | [Solution](solutions/1408-string-matching-in-an-array/solution.cpp) |
9494
| 2025-01-08 | 3042 | [Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/) | [Solution](solutions/3042-count-prefix-and-suffix-pairs-i/solution.cpp) |
9595
| 2025-01-09 | 2185 | [Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | [Solution](solutions/2185-counting-words-with-a-given-prefix/solution.cpp) |
96+
| 2025-01-10 | 916 | [Word Subsets](https://leetcode.com/problems/word-subsets/) | [Solution](solutions/916-word-subsets/solution.cpp) |
9697

9798
## Personal Goals
9899

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
private:
3+
// Helper function to count character frequencies in a string
4+
vector<int> getCharCount(const string &word) {
5+
vector<int> count(26, 0);
6+
for (char c : word) {
7+
count[c - 'a']++;
8+
}
9+
return count;
10+
}
11+
12+
public:
13+
vector<string> wordSubsets(vector<string> &words1, vector<string> &words2) {
14+
// Get maximum frequency required for each character from words2
15+
vector<int> maxFreq(26, 0);
16+
for (const string &word : words2) {
17+
vector<int> currFreq = getCharCount(word);
18+
for (int i = 0; i < 26; i++) {
19+
maxFreq[i] = max(maxFreq[i], currFreq[i]);
20+
}
21+
}
22+
23+
// Check each word in words1
24+
vector<string> result;
25+
for (const string &word : words1) {
26+
vector<int> freq = getCharCount(word);
27+
bool isUniversal = true;
28+
29+
// Check if word has sufficient frequency for each required character
30+
for (int i = 0; i < 26; i++) {
31+
if (freq[i] < maxFreq[i]) {
32+
isUniversal = false;
33+
break;
34+
}
35+
}
36+
37+
if (isUniversal) {
38+
result.push_back(word);
39+
}
40+
}
41+
42+
return result;
43+
}
44+
};

0 commit comments

Comments
 (0)