Analyze User Website Visit Pattern Problem
Description
LeetCode Problem 1152.
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 usersxsuch thatxvisited"home"then visited"away"and visited"love"after that. - Similarly, if the pattern is
["leetcode", "love", "leetcode"], the score is the number of usersxsuch thatxvisited"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 usersxsuch thatxvisited"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.
Example 1:
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).
Example 2:
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 <= 501 <= username[i].length <= 10timestamp.length == username.length1 <= timestamp[i] <= 10^9website.length == username.length1 <= website[i].length <= 10username[i]andwebsite[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
Approach: Enumerate Each User's Patterns
The input is small (at most 50 visits), so we can simply list every pattern each user followed and count.
- Order the visits. Sort the visits by timestamp and group them by user. Each user now has the list of websites they visited, in order.
- Collect each user's patterns. For a user with
mvisits, every choice of indicesi < j < kgives a pattern(sites[i], sites[j], sites[k]). Put them in a set first. The score counts users, not ways: a user who visited"a"five times follows["a","a","a"]in ten different ways, but adds only1to its score. - Count. Add
1to the score of each pattern in the user's set. - Pick the answer. Take the highest score. Among patterns with that score, take the lexicographically smallest one, compared website by website.
For the tie-break, some versions store a pattern as the string "a,b,c" and compare those strings. That works because ',' sorts before every lowercase letter, so a website that is a prefix of another ("a" and "ab") still compares as smaller, just as in the word-by-word comparison.
Complexity: O(n3) time and space in the worst case (one user with all n visits has about n3/6 index triples), plus O(n log n) for sorting.
import collections
class Solution:
def mostVisitedPattern(self, username: List[str], timestamp: List[int], website: List[str]) -> List[str]:
# Each user's websites in the order they were visited.
visits = collections.defaultdict(list)
for _, user, site in sorted(zip(timestamp, username, website)):
visits[user].append(site)
score = collections.Counter()
for sites in visits.values():
m = len(sites)
# A user adds 1 to each distinct pattern they followed, however many ways they followed it.
patterns = {(sites[i], sites[j], sites[k])
for i in range(m) for j in range(i + 1, m) for k in range(j + 1, m)}
score.update(patterns)
# Highest score first, then the lexicographically smallest pattern.
best = min(score, key=lambda p: (-score[p], p))
return list(best)