forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2215-find-the-difference-of-two-arrays.py
40 lines (30 loc) · 1.12 KB
/
2215-find-the-difference-of-two-arrays.py
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
# Time Complexiy: O(m + n)
# Space Complexity: O(m + n)
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
nums1 = set(nums1)
nums2 = set(nums2)
table = {}
for _, val in enumerate(nums2):
table[val] = 1
unik1 = []
unik2 = []
for i in nums1:
if i in table:
table[i] += 1
else:
unik1.append(i)
for key, val in table.items():
if val == 1:
unik2.append(key)
return [unik1, unik2]
# Time Complexity: O(m + n), we check each element of nums1Set and nums2Set
# Space Complexity: O(m + n), where m and n are length sets in worst case.
from typing import List # ignore this, just for typing
class Solution:
def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:
nums1_set = set(nums1)
nums2_set = set(nums2)
lst1 = [num for num in nums1_set if num not in nums2_set]
lst2 = [num for num in nums2_set if num not in nums1_set]
return [lst1, lst2]