MediumNo. 1152LeetCode Premium

Analyze User Website Visit Pattern — LeetCode 1152 Python Solution

Pattern
Sorting
Topics
Array, Hash Table, String, Sorting
Reading time
4 min

The problem

You are given two string arrays username and website and an integer array timestamp. All the given arrays are of the same length and the tuple [username[i], website[i], timestamp[i]] indicates that the user username[i] visited the website website[i] at time timestamp[i].

Example

Input
username = ["joe","joe","joe","james","james","james","james","mary","mary","mary"], timestamp = [1,2,3,4,5,6,7,8,9,10], website = ["home","about","career","home","cart","maps","home","home","about","career"]
Output
["home","about","career"]
Explanation
The tuples in this example are:

Python solution

PythonSolution
class Solution:
    def mostVisitedPattern(
        self, username: List[str], timestamp: List[int], website: List[str]
    ) -> List[str]:
        d = defaultdict(list)
        for user, _, site in sorted(
            zip(username, timestamp, website), key=lambda x: x[1]
        ):
            d[user].append(site)

        cnt = Counter()
        for sites in d.values():
            m = len(sites)
            s = set()
            if m > 2:
                for i in range(m - 2):
                    for j in range(i + 1, m - 1):
                        for k in range(j + 1, m):
                            s.add((sites[i], sites[j], sites[k]))
            for t in s:
                cnt[t] += 1
        return sorted(cnt.items(), key=lambda x: (-x[1], x[0]))[0][0]

Complexity

Time
O(n3)
Space · auxiliary
O(n3)

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 1152. Analyze User Website Visit Pattern is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0049Group AnagramsMedium
0522Longest Uncommon Subsequence IIMedium
0692Top K Frequent WordsMedium
0720Longest Word in DictionaryMedium
0721Accounts MergeMedium
0792Number of Matching SubsequencesMedium

Frequently asked questions

How hard is LeetCode 1152. Analyze User Website Visit Pattern?

LeetCode 1152. Analyze User Website Visit Pattern is rated Medium on LeetCode.

What is the time complexity of LeetCode 1152. Analyze User Website Visit Pattern?

The Python solution on this page runs in O(n3).

What is the space complexity of LeetCode 1152. Analyze User Website Visit Pattern?

The Python solution on this page uses O(n3) auxiliary space.

What topics does LeetCode 1152. Analyze User Website Visit Pattern cover?

LeetCode 1152. Analyze User Website Visit Pattern is tagged Array, Hash Table, String and Sorting on LeetCode.

Is LeetCode 1152. Analyze User Website Visit Pattern a premium problem?

Yes. LeetCode 1152. Analyze User Website Visit Pattern is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.