1. Hash Map

class Solution:
    def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
        arr = list(zip(timestamp, username, website))
        arr.sort()

        mp = defaultdict(list)
        for time, user, site in arr:
            mp[user].append(site)

        count = defaultdict(int)
        for user in mp:
            patterns = set()
            cur = mp[user]
            for i in range(len(cur)):
                for j in range(i + 1, len(cur)):
                    for k in range(j + 1, len(cur)):
                        patterns.add((cur[i], cur[j], cur[k]))
            for p in patterns:
                count[p] += 1

        max_count = 0
        res = tuple()
        for pattern in count:
            if count[pattern] > max_count or (count[pattern] == max_count and pattern < res):
                max_count = count[pattern]
                res = pattern

        return list(res)

Time & Space Complexity

  • Time complexity: O(nlogn+nu+n3w)O(n \log n + n * u + n ^ 3 * w)
  • Space complexity: O(nu+n3w)O(n * u + n ^ 3 * w)

Where nn is the size of the array timestamptimestamp, uu is the maximum length of any string in the array usernameusername, and ww is the maximum length of any string in the array websitewebsite.