KatInterview

Longest Unique Sequence of Media Titles

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringssliding-windowhash-tablestrings

Last updated

Find the maximum length of a contiguous subsequence of string titles containing no duplicated elements using a dynamic sliding window approach.

Given a list of strings shows representing a sequence of media titles, determine the length of the longest contiguous sublist that contains no duplicate titles.

Constraints

  • 0 <= len(shows) <= 10^5
  • Each element in shows is a non-empty string consisting of printable ASCII characters.
  • String matching is exact (case-sensitive).

Examples

Example 1

Input: longest_unique_sequence(['A', 'B', 'C', 'A', 'B', 'D'])

Output: 4

Example 2

Input: longest_unique_sequence(['p', 'w', 'w', 'k', 'e', 'w'])

Output: 3

Example 3

Input: longest_unique_sequence(['x', 'y', 'z'])

Output: 3

Hints

Hint 1

Consider using a sliding window bounded by a start pointer and an end pointer to maintain a contiguous sublist.

Hint 2

To achieve linear time complexity, use a hash map to record the most recent index where each item appeared.

Hint 3

When you encounter an item already seen, check if its last reported position is within your current window before moving the start pointer.