> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Analyze User Website Visit Pattern

> Tested Python solution for LeetCode 1152 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1152, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/analyze-user-website-visit-pattern/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1152   # by problem number
lcpy gen -s analyze_user_website_visit_pattern   # by problem name
```

## 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]`.

A **pattern** is a list of three websites (not necessarily distinct).

* For example, `['home', 'away', 'love']`, `['leetcode', 'love', 'leetcode']`, and `['luffy', 'luffy', 'luffy']` are all patterns.

The **score** of a **pattern** is the number of users that visited all the websites in the pattern in the same order they appeared in the pattern.

* For example, if the pattern is `['home', 'away', 'love']`, the score is the number of users `x` such that `x` visited `'home'` then visited `'away'` and visited `'love'` after that.
* Similarly, if the pattern is `['leetcode', 'love', 'leetcode']`, the score is the number of users `x` such that `x` visited `'leetcode'` then visited `'love'` and visited `'leetcode'` one more time after that.
* Also, if the pattern is `['luffy', 'luffy', 'luffy']`, the score is the number of users `x` such that `x` visited `'luffy'` three different times at different timestamps.

Return the **pattern** with the largest **score**. If there is more than one pattern with the same largest score, return the lexicographically smallest such pattern.

Note that the websites in a pattern **do not** need to be visited *contiguously*, they only need to be visited in the order they appeared in the pattern.

### Examples

```
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:
['joe','home',1],['joe','about',2],['joe','career',3],['james','home',4],['james','cart',5],['james','maps',6],['james','home',7],['mary','home',8],['mary','about',9], and ['mary','career',10].
The pattern ('home', 'about', 'career') has score 2 (joe and mary).
The pattern ('home', 'cart', 'maps') has score 1 (james).
The pattern ('home', 'cart', 'home') has score 1 (james).
The pattern ('home', 'maps', 'home') has score 1 (james).
The pattern ('cart', 'maps', 'home') has score 1 (james).
The pattern ('home', 'home', 'home') has score 0 (no user visited home 3 times).
```

```
Input: username = ['ua','ua','ua','ub','ub','ub']
timestamp = [1,2,3,4,5,6]
website = ['a','b','a','a','b','c']
Output: ['a','b','a']
```

### Constraints

* 3 \<= username.length \<= 50
* 1 \<= username\[i].length \<= 10
* timestamp.length == username.length
* 1 \<= timestamp\[i] \<= 10^9
* website.length == username.length
* 1 \<= website\[i].length \<= 10
* username\[i] and website\[i] consist of lowercase English letters.
* It is guaranteed that there is at least one user who visited at least three websites.
* All the tuples `[username[i], timestamp[i], website[i]]` are **unique**.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/analyze_user_website_visit_pattern/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/analyze_user_website_visit_pattern/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from collections import Counter, defaultdict


class Solution:
    # Time: O(n^3) per user in the worst case, n = visits per user
    # Space: O(n^3) for the distinct triplets
    def most_visited_pattern(
        self, username: list[str], timestamp: list[int], website: list[str]
    ) -> list[str]:
        visits: dict[str, list[tuple[int, str]]] = defaultdict(list)
        for user, ts, site in zip(username, timestamp, website, strict=True):
            visits[user].append((ts, site))

        scores: Counter[tuple[str, str, str]] = Counter()
        for entries in visits.values():
            sites = [site for _, site in sorted(entries)]
            count = len(sites)
            patterns = {
                (sites[i], sites[j], sites[k])
                for i in range(count - 2)
                for j in range(i + 1, count - 1)
                for k in range(j + 1, count)
            }
            scores.update(patterns)

        best = min(scores.items(), key=lambda item: (-item[1], item[0]))[0]
        return list(best)
```

## Complexity

| Time | Space |
| - | - |
| O(n^3) per user in the worst case, n = visits per user | O(n^3) for the distinct triplets |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.