Mid/Senior

Analyze User Website Visit Pattern

You are given three arrays username, timestamp, and website of the same length. The ith entry represents that user username[i] visited website website[i] at time timestamp[i].

A 3-sequence pattern is a list of three websites, where the websites do not have to be distinct. For example, ["home", "about", "career"] is a pattern.

The score of a pattern is the number of users who visited all three websites in that pattern in the same chronological order. The visits do not need to be consecutive, but they must appear in increasing timestamp order for that user. A user contributes at most once to the score of a given pattern, even if they visited that pattern multiple times.

Return the 3-sequence pattern with the highest score. If multiple patterns have the same highest score, return the lexicographically smallest such pattern.

Example 1
Inputusername = ["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"]
Both joe and mary visited home, then about, then career, giving that pattern the highest score of 2.
Example 2
Inputusername = ["ua","ua","ua","ub","ub","ub"], timestamp = [1,2,3,4,5,6], website = ["a","b","a","a","b","c"]
Output["a","b","a"]
The patterns ["a","b","a"] and ["a","b","c"] each have score 1, so the lexicographically smaller pattern is returned.

Constraints

  • 3 <= username.length <= 50
  • timestamp.length == username.length
  • website.length == username.length
  • 1 <= username[i].length <= 10
  • 1 <= website[i].length <= 10
  • 1 <= timestamp[i] <= 10^9
  • username[i] and website[i] consist of lowercase English letters
  • The given list contains no duplicate timestamp
  • It is guaranteed that at least one user visited at least 3 websites

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate