-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay39.java
More file actions
42 lines (34 loc) · 1 KB
/
Copy pathDay39.java
File metadata and controls
42 lines (34 loc) · 1 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
/*Array Subset
Given two arrays a[] and b[], your task is to determine whether b[] is a subset of a[].
Examples:
Input: a[] = [11, 7, 1, 13, 21, 3, 7, 3], b[] = [11, 3, 7, 1, 7]
Output: true
Explanation: b[] is a subset of a[]
Input: a[] = [1, 2, 3, 4, 4, 5, 6], b[] = [1, 2, 4]
Output: true
Explanation: b[] is a subset of a[]
Input: a[] = [10, 5, 2, 23, 19], b[] = [19, 5, 3]
Output: false
Explanation: b[] is not a subset of a[] */
/* import java.util.*;
class Solution {
public boolean isSubset(int a[], int b[]) {
HashMap<Integer, Integer> map = new HashMap<>();
// Count frequency of elements in a[]
for (int num : a) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// Check elements of b[]
for (int num : b) {
if (!map.containsKey(num) || map.get(num) == 0) {
return false;
}
map.put(num, map.get(num) - 1);
}
return true;
}
}
*/
/*⏱ Complexity
Time: O(n + m)
Space: O(n) */