#31Browser History — Longest Unique Website Visit Sequence
A browser analytics system records a user's website visits in chronological order. You are given a string visits, where each character represents a website identifier visited by the user. A continuous browsing session is a consecutive sequence of website visits.
Find the length of the longest continuous browsing session in which no website is visited more than once. A website may appear multiple times in the complete browsing history, but within the selected continuous session, every website must be unique.
Rules The selected visits must be consecutive. A website cannot appear more than once in the selected sequence. The order of visits must be preserved. Return only the maximum length. Different sessions may have the same maximum length.
Real-World Applications Browser Analytics – Find the longest browsing session where the user does not revisit the same website. Recommendation Systems – Analyze consecutive content or websites viewed by a user to measure content diversity. User Behavior Analysis – Identify periods where a user explores many different websites without repetition. Cybersecurity – Detect unusual browsing patterns by analyzing continuous sequences of website visits. Web Session Analysis – Measure the maximum number of unique websites visited consecutively during a session.
Examples
Example 1
Input: visits = "abcad"
Output: 4
Explanation: The longest unique browsing sequence is "bcad", containing four different websites.
Example 2
Input: visits = "aaab"
Output: 2
Explanation: The longest unique sequence is "ab".
Example 3
Input: visits = "xyzzabc"
Output: 4
Explanation: The longest unique sequence is "zabc", which contains four different website identifiers.
Constraints
- 0 <= visits.length <= 100000
- visits consists of uppercase English letters, lowercase English letters, digits, spaces, and common symbols.
- Each character represents a website identifier.
- The input may contain repeated website identifiers.
- The answer fits within a 32-bit signed integer.
