Find Trending Hashtags II Problem
#3103
Description
LeetCode Problem 3103.
Table: Tweets
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| tweet_id | int |
| tweet_date | date |
| tweet | varchar |
+-------------+---------+
tweet_id is the primary key (column with unique values) for this table.
Each row of this table contains user_id, tweet_id, tweet_date and tweet.
It is guaranteed that all tweet_date are valid dates in February 2024.
Write a solution to find the top 3 trending hashtags in February 2024. Every tweet may contain several hashtags.
Return the result table ordered by count of hashtag, hashtag in descending order.
The result format is in the following example.
Example 1:
Input:
Tweets table:
+---------+----------+------------------------------------------------------------+------------+
| user_id | tweet_id | tweet | tweet_date |
+---------+----------+------------------------------------------------------------+------------+
| 135 | 13 | Enjoying a great start to the day. #HappyDay #MorningVibes | 2024-02-01 |
| 136 | 14 | Another #HappyDay with good vibes! #FeelGood | 2024-02-03 |
| 137 | 15 | Productivity peaks! #WorkLife #ProductiveDay | 2024-02-04 |
| 138 | 16 | Exploring new tech frontiers. #TechLife #Innovation | 2024-02-04 |
| 139 | 17 | Gratitude for today's moments. #HappyDay #Thankful | 2024-02-05 |
| 140 | 18 | Innovation drives us. #TechLife #FutureTech | 2024-02-07 |
| 141 | 19 | Connecting with nature's serenity. #Nature #Peaceful | 2024-02-09 |
+---------+----------+------------------------------------------------------------+------------+
Output:
+-----------+-------+
| hashtag | count |
+-----------+-------+
| #HappyDay | 3 |
| #TechLife | 2 |
| #WorkLife | 1 |
+-----------+-------+
Explanation:
- #HappyDay: Appeared in tweet IDs 13, 14, and 17, with a total count of 3 mentions.
- #TechLife: Appeared in tweet IDs 16 and 18, with a total count of 2 mentions.
- #WorkLife: Appeared in tweet ID 15, with a total count of 1 mention.
Note: Output table is sorted in descending order by count and hashtag respectively.
Solution
Approach: Extract every hashtag with a regular expression, then count
A tweet can carry several hashtags, so the first job is to turn each tweet into one row per hashtag. Everything after that is an ordinary GROUP BY.
- A hashtag is a
#followed by word characters, so the pattern#[A-Za-z0-9_]+finds one.REGEXP_SUBSTR(tweet, pattern, 1, n)returns then-th match in a tweet, orNULLwhen there are fewer thann. - Join every tweet with the numbers
1, 2, 3, ...(a small recursive CTE, as long as the longest tweet since a hashtag needs at least two characters) and keep the rows where the match is notNULL. That yields one row per hashtag occurrence. - Count rows per hashtag, order by the count and then by the hashtag, both descending, and keep three rows.
The date filter is already satisfied (every tweet is from February 2024), but it costs nothing to keep it. The tie-break matters: with equal counts the larger hashtag text comes first.
The Pandas version does the same with str.findall, flattens the lists with explode, and uses value_counts.
WITH RECURSIVE nums AS (
SELECT 1 AS n
UNION ALL
SELECT n + 1
FROM nums
WHERE n < (SELECT MAX(CHAR_LENGTH(tweet)) DIV 2 + 1 FROM Tweets)
),
tags AS (
SELECT REGEXP_SUBSTR(t.tweet, '#[A-Za-z0-9_]+', 1, nums.n) AS hashtag
FROM Tweets AS t
JOIN nums
WHERE t.tweet_date BETWEEN '2024-02-01' AND '2024-02-29'
)
SELECT hashtag, COUNT(*) AS `count`
FROM tags
WHERE hashtag IS NOT NULL
GROUP BY hashtag
ORDER BY `count` DESC, hashtag DESC
LIMIT 3;