Problem · String

Top 3 Trending Hashtags in a Time Window

Learn this problem
EasyF5 logoF5INTERNOA

Problem statement

You are given two arrays of equal length: tweets and timestamps. The i-th tweet was published at time timestamps[i] and contains text tweets[i].

Within the inclusive interval [currentTime - timeWindow, currentTime], find the top at most three hashtags by total number of occurrences.

A hashtag starts with # and continues through consecutive letters, digits, or underscores. Stop the hashtag when any other character is encountered.

Sort the answer by count descending, then lexicographically ascending for ties. Return each hashtag with its leading #.

Function

topTrendingHashtags(tweets: String[], timestamps: int[], currentTime: int, timeWindow: int) → String[]

Complete the function topTrendingHashtags in the editor below.

topTrendingHashtags has the following parameters:

  1. String[] tweets: the tweet texts
  2. int[] timestamps: publish times
  3. int currentTime: the right end of the window
  4. int timeWindow: the window width

Returns

String[]: the top at most three hashtags in ranked order.

Examples

Example 1

tweets = ["hi #a #b", "#a !!!", "no tag", "#b #b"]timestamps = [1, 2, 3, 4]currentTime = 4timeWindow = 2return = ["#b", "#a"]

The active window is timestamps 2 through 4. #b appears twice and #a appears once.

Example 2

tweets = ["#z #a", "#a", "#z"]timestamps = [1, 2, 3]currentTime = 3timeWindow = 3return = ["#a", "#z"]

Both hashtags appear twice, so lexicographical order breaks the tie.

Constraints

  • 1 <= tweets.length == timestamps.length <= 2 * 10^5
  • 0 <= timestamps[i] <= 10^9
  • sum(len(tweets[i])) <= 10^6
drafts saved locally
public String[] topTrendingHashtags(String[] tweets, int[] timestamps, int currentTime, int timeWindow) {
    // write your code here
}
tweets["hi #a #b", "#a !!!", "no tag", "#b #b"]
timestamps[1, 2, 3, 4]
currentTime4
timeWindow2
expected["#b", "#a"]
checking account