aboutsummaryrefslogtreecommitdiff
path: root/other/word_subsets.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/word_subsets.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/word_subsets.cpp')
-rw-r--r--other/word_subsets.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/other/word_subsets.cpp b/other/word_subsets.cpp
new file mode 100644
index 0000000..d053375
--- /dev/null
+++ b/other/word_subsets.cpp
@@ -0,0 +1,57 @@
+// 916. Word Subsets
+#include "leetcode.h"
+
+/*
+ * you are given two string arrays 'words1' and 'words2'. a string 'b' is a
+ * subset of string 'a' if every letter in 'b' occurs in 'a' including
+ * multiplicity. a string 'a' from 'words1' is universal if for every string 'b'
+ * in 'words2', 'b' is a subset of 'a'. return an array of all the universal
+ * strins in 'words1'. you may return the answer in any order.
+ */
+
+class Solution {
+public:
+ vector<string> wordSubsets(vector<string> &words1, vector<string> &words2) {
+ vector<int> cnt(26), tmp(26);
+ int i;
+ for (string b : words2) {
+ tmp = counter(b);
+ for (i = 0; i < 26; ++i)
+ cnt[i] = max(cnt[i], tmp[i]);
+ }
+ vector<string> ans;
+ for (string a : words1) {
+ tmp = counter(a);
+ for (i = 0; i < 26; ++i)
+ if (tmp[i] < cnt[i])
+ break;
+ if (i == 26)
+ ans.push_back(a);
+ }
+ return ans;
+ }
+
+private:
+ vector<int> counter(string &word) {
+ vector<int> cnt(26);
+ for (char c : word)
+ cnt[c - 'a']++;
+ return cnt;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<string> w11 = {"amazon", "apple", "facebook", "google", "leetcode"},
+ w21 = {"e", "o"};
+ vector<string> w12 = {"amazon", "apple", "facebook", "google", "leetcode"},
+ w22 = {"l", "e"};
+ vector<string> ws1 = obj.wordSubsets(w11, w21),
+ ws2 = obj.wordSubsets(w12, w22);
+ for (auto i : ws1)
+ cout << i << ' '; // expect: ["facebook","google","leetcode"]
+ printf("\n");
+ for (auto i : ws2)
+ cout << i << ' '; // expect: ["apple","google","leetcode"]
+ printf("\n");
+}