FastPrepFastPrep
Problem Brief

Top 3 Trending Hashtags in a Time Window

INTERNOA

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 Description

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.

1Example 1

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

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

2Example 2

Input
tweets = ["#z #a", "#a", "#z"], timestamps = [1, 2, 3], currentTime = 3, timeWindow = 3
Output
["#a", "#z"]
Explanation

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

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= tweets.length == timestamps.length <= 2 * 10^5
  • 0 <= timestamps[i] <= 10^9
  • sum(len(tweets[i])) <= 10^6
public String[] topTrendingHashtags(String[] tweets, int[] timestamps, int currentTime, int timeWindow) {
    // write your code here
}
Input

tweets

["hi #a #b", "#a !!!", "no tag", "#b #b"]

timestamps

[1, 2, 3, 4]

currentTime

4

timeWindow

2

Output

["#b", "#a"]

Sign in to submit your solution.