|
| 1 | +# [3087. Find Trending Hashtags](https://leetcode.com/problems/find-trending-hashtags) |
| 2 | + |
| 3 | +[中文文档](/solution/3000-3099/3087.Find%20Trending%20Hashtags/README.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## Description |
| 8 | + |
| 9 | +<p>Table: <code>Tweets</code></p> |
| 10 | + |
| 11 | +<pre> |
| 12 | ++-------------+---------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+---------+ |
| 15 | +| user_id | int | |
| 16 | +| tweet_id | int | |
| 17 | +| tweet_date | date | |
| 18 | +| tweet | varchar | |
| 19 | ++-------------+---------+ |
| 20 | +tweet_id is the primary key (column with unique values) for this table. |
| 21 | +Each row of this table contains user_id, tweet_id, tweet_date and tweet. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p>Write a solution to find the <strong>top</strong> <code>3</code> trending <strong>hashtags</strong> in <strong>February</strong> <code>2024</code>.</p> |
| 25 | + |
| 26 | +<p>Return <em>the result table orderd by count of hashtag, hastag in </em><strong>descending</strong><em> order.</em></p> |
| 27 | + |
| 28 | +<p>The result format is in the following example.</p> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong class="example">Example 1:</strong></p> |
| 32 | + |
| 33 | +<div class="example-block"> |
| 34 | +<p><strong>Input:</strong></p> |
| 35 | + |
| 36 | +<p>Tweets table:</p> |
| 37 | + |
| 38 | +<pre class="example-io"> |
| 39 | ++---------+----------+----------------------------------------------+------------+ |
| 40 | +| user_id | tweet_id | tweet | tweet_date | |
| 41 | ++---------+----------+----------------------------------------------+------------+ |
| 42 | +| 135 | 13 | Enjoying a great start to the day! #HappyDay | 2024-02-01 | |
| 43 | +| 136 | 14 | Another #HappyDay with good vibes! | 2024-02-03 | |
| 44 | +| 137 | 15 | Productivity peaks! #WorkLife | 2024-02-04 | |
| 45 | +| 138 | 16 | Exploring new tech frontiers. #TechLife | 2024-02-04 | |
| 46 | +| 139 | 17 | Gratitude for today's moments. #HappyDay | 2024-02-05 | |
| 47 | +| 140 | 18 | Innovation drives us. #TechLife | 2024-02-07 | |
| 48 | +| 141 | 19 | Connecting with nature's serenity. #Nature | 2024-02-09 | |
| 49 | ++---------+----------+----------------------------------------------+------------+ |
| 50 | + </pre> |
| 51 | + |
| 52 | +<p><strong>Output:</strong></p> |
| 53 | + |
| 54 | +<pre class="example-io"> |
| 55 | ++-----------+--------------+ |
| 56 | +| hashtag | hashtag_count| |
| 57 | ++-----------+--------------+ |
| 58 | +| #HappyDay | 3 | |
| 59 | +| #TechLife | 2 | |
| 60 | +| #WorkLife | 1 | |
| 61 | ++-----------+--------------+ |
| 62 | + |
| 63 | +</pre> |
| 64 | + |
| 65 | +<p><strong>Explanation:</strong></p> |
| 66 | + |
| 67 | +<ul> |
| 68 | + <li><strong>#HappyDay:</strong> Appeared in tweet IDs 13, 14, and 17, with a total count of 3 mentions.</li> |
| 69 | + <li><strong>#TechLife:</strong> Appeared in tweet IDs 16 and 18, with a total count of 2 mentions.</li> |
| 70 | + <li><strong>#WorkLife:</strong> Appeared in tweet ID 15, with a total count of 1 mention.</li> |
| 71 | +</ul> |
| 72 | + |
| 73 | +<p><b>Note:</b> Output table is sorted in descending order by hashtag_count and hashtag respectively.</p> |
| 74 | +</div> |
| 75 | + |
| 76 | +## Solutions |
| 77 | + |
| 78 | +### Solution 1: Extract Substring + Grouping |
| 79 | + |
| 80 | +We can query all tweets from February 2024, use the `SUBSTRING_INDEX` function to extract Hashtags, then use the `GROUP BY` and `COUNT` functions to count the occurrences of each Hashtag. Finally, we sort by the number of occurrences in descending order and by Hashtag in descending order, and take the top three popular Hashtags. |
| 81 | + |
| 82 | +<!-- tabs:start --> |
| 83 | + |
| 84 | +```sql |
| 85 | +# Write your MySQL query statement below |
| 86 | +SELECT |
| 87 | + CONCAT('#', SUBSTRING_INDEX(SUBSTRING_INDEX(tweet, '#', -1), ' ', 1)) AS hashtag, |
| 88 | + COUNT(1) AS hashtag_count |
| 89 | +FROM Tweets |
| 90 | +WHERE DATE_FORMAT(tweet_date, '%Y%m') = '202402' |
| 91 | +GROUP BY 1 |
| 92 | +ORDER BY 2 DESC, 1 DESC |
| 93 | +LIMIT 3; |
| 94 | +``` |
| 95 | + |
| 96 | +```python |
| 97 | +import pandas as pd |
| 98 | + |
| 99 | + |
| 100 | +def find_trending_hashtags(tweets: pd.DataFrame) -> pd.DataFrame: |
| 101 | + tweets = tweets[tweets["tweet_date"].dt.strftime("%Y%m") == "202402"] |
| 102 | + tweets["hashtag"] = "#" + tweets["tweet"].str.extract(r"#(\w+)") |
| 103 | + hashtag_counts = tweets["hashtag"].value_counts().reset_index() |
| 104 | + hashtag_counts.columns = ["hashtag", "hashtag_count"] |
| 105 | + hashtag_counts = hashtag_counts.sort_values( |
| 106 | + by=["hashtag_count", "hashtag"], ascending=[False, False] |
| 107 | + ) |
| 108 | + top_3_hashtags = hashtag_counts.head(3) |
| 109 | + return top_3_hashtags |
| 110 | +``` |
| 111 | + |
| 112 | +<!-- tabs:end --> |
| 113 | + |
| 114 | +<!-- end --> |
0 commit comments