-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathArray subset of another array.cpp
More file actions
46 lines (37 loc) · 1.07 KB
/
Array subset of another array.cpp
File metadata and controls
46 lines (37 loc) · 1.07 KB
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
/*
link: https://practice.geeksforgeeks.org/problems/array-subset-of-another-array2317/1#
*/
// ----------------------------------------------------------------------------------------------------------------------- //
// TC: O(N)
// SC: O(N)
string isSubset(int a1[], int a2[], int n, int m) {
unordered_set<int> s;
for (int i = 0;i < n;i++) {
s.insert(a1[i]);
}
for (int i = 0;i < m;i++) {
if (s.find(a2[i]) == s.end()) return "No";
}
return "Yes";
}
// ----------------------------------------------------------------------------------------------------------------------- //
// similar solution.
string isSubset(int a1[], int a2[], int n, int m) {
unordered_map<int, int> um, um2;
for (int i = 0; i < n; i++) {
um[a1[i]]++;
}
for (int i = 0; i < m; i++) {
um2[a2[i]]++;
}
int count = 0;
for (auto it = um2.begin(); it != um2.end(); it++) {
if (um[it->first] >= it->second) {
count++;
}
}
if (count == m)
return "Yes";
else
return "No";
}