[
array
hash table
sort
counter
]
Leetcode 1213 Intersection of Three Sorted Arrays
Problem statement
https://leetcode.com/problems/last-substring-in-lexicographical-order/
Solution
We can just put all arrays to sets and find intersection and then sort answer.
Complexity
It is O(n + m log m)
, where n
is total number of elements and m
is number of elements in the smallest array.
Code
class Solution:
def arraysIntersection(self, A1, A2, A3):
return sorted(set(A1) & set(A2) & set(A3))
Remark
Or we can use counters to calculate number of times each element is taken and take only those which have frequency 3
: time complexity will be O(n)
.