class Solution:
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
""" tc O(NlgN + N*(N-1)*(N-2)/3! ) sc O(N*(N-1)*(N-2)/3!)
1. sort input by username and timestamp
2. put all the visited sites onto history list by username
3. for each history list, get its combination with size 3 to count the occurance of each sub sequesnce
4. get the lexically smallest subsequence with max occurance
knowledge to know: update a counter with list of keys using update(list_keys )
"""
user_vs_web = collections.defaultdict(list)
for _, user, web in sorted(zip(timestamp, username, website)):
user_vs_web[user].append(web)
cnt = collections.Counter()
for each_list in user_vs_web.values():
tmp_cnt = Counter(set(combinations(each_list,3))) # deduplicate subsequence
cnt += tmp_cnt
#cnt.update(tmp_cnt)
return list(min(cnt, key = lambda x :(-cnt[x], x)))
#return list((max(sorted(cnt), key= cnt.get))) # put "dict" into sorted(), and only return keys