Skip to main content

Find Trending Hashtags - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase5 min read
Practice this problem

Problem Statement

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.

Write a solution to find the top 3 trending hashtags in February 2024. Each tweet only contains one hashtag.

Return the result table orderd 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 | 2024-02-01 |
| 136     | 14       | Another #HappyDay with good vibes!           | 2024-02-03 |
| 137     | 15       | Productivity peaks! #WorkLife                | 2024-02-04 |
| 138     | 16       | Exploring new tech frontiers. #TechLife      | 2024-02-04 |
| 139     | 17       | Gratitude for today's moments. #HappyDay     | 2024-02-05 |
| 140     | 18       | Innovation drives us. #TechLife              | 2024-02-07 |
| 141     | 19       | Connecting with nature's serenity. #Nature   | 2024-02-09 |
+---------+----------+----------------------------------------------+------------+
 

Output:

+-----------+--------------+
| hashtag   | 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 hashtag_count and hashtag respectively.

Approach Overview

Problem Overview: You are given tweet text that may contain hashtags (words starting with #). The task is to extract those hashtags, count how often each appears, and return the ones that are trending based on frequency.

Approach 1: Extract Substring + Grouping (O(n * m) time, O(k) space)

Scan each tweet and extract every token that starts with #. In SQL, this typically uses string functions or regex to isolate hashtags, followed by GROUP BY to count occurrences. In Python, split the tweet text into tokens, filter tokens beginning with #, and increment counts in a dictionary. The key insight is that trending hashtags are simply the most frequent ones after normalizing and aggregating all hashtag occurrences.

After extraction, aggregate using a hash-based structure such as a dictionary or SQL grouping. Each unique hashtag becomes a key, and every occurrence increments its frequency. Finally, sort the results by frequency in descending order (and lexicographically if ties are required). This approach processes every tweet once and performs constant‑time updates per hashtag.

This pattern appears frequently in database aggregation problems and text processing tasks. The extraction step is essentially a string parsing problem, while the counting step relies on hash-based grouping similar to a hash map frequency table.

Approach 2: Regex Extraction + Frequency Map (O(n * m) time, O(k) space)

Instead of manually splitting tokens, use a regular expression such as #\w+ to directly extract hashtags from each tweet. Regex engines scan the string and return all matches, which are then aggregated in a frequency map or grouped in SQL. This reduces edge cases when punctuation or mixed formatting appears in the tweet text.

The counting and sorting steps remain identical. Each extracted hashtag increments its counter, and the final list is ordered by descending frequency. Regex-based extraction is often cleaner in Python and many SQL engines that support REGEXP_SUBSTR or similar functions.

Recommended for interviews: The substring extraction plus grouping approach is what interviewers expect. It shows you understand how to parse structured tokens from text and aggregate them efficiently. A simple brute-force scan demonstrates the core idea, but the optimal solution uses hash-based counting or SQL GROUP BY to compute frequencies in linear time relative to the input size.

Solution

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.

Code

MySQL

Python

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Extract Substring + GroupingO(n * m)O(k)Standard SQL or Python implementation when hashtags can be tokenized from tweet text
Regex Extraction + Frequency MapO(n * m)O(k)Cleaner parsing when tweets contain punctuation or irregular spacing

Video Solution

Leetcode MEDIUM 3087 - Find Trending Hashtags REGEX Explained - Solved by Everyday Data Science • Everyday Data Science • 1,131 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Find Trending Hashtags easy or hard?
Find Trending Hashtags is considered a Medium problem. The difficulty comes from correctly extracting hashtags from text and performing efficient aggregation. Once the parsing logic is clear, the counting step is straightforward using hash maps or SQL grouping.
Find Trending Hashtags Python/Java solution
In Python, split the tweet text or use regex to extract hashtags, store counts in a dictionary, and sort by frequency. In SQL (commonly MySQL), use substring or regex extraction functions and aggregate with GROUP BY and COUNT to compute trending hashtags.
How to solve Find Trending Hashtags in O(n)?
Treat the total input size as the number of characters across all tweets. Scan each tweet once, extract tokens beginning with '#', and update a hash map or SQL aggregation. Each hashtag update is O(1), so the algorithm is linear relative to the total text processed.
What is the best approach for Find Trending Hashtags?
The most effective approach extracts hashtags from each tweet and aggregates them using a frequency count. In SQL, this means parsing the hashtag substring and using GROUP BY to count occurrences. In Python, tokenize the text and update a dictionary for each hashtag. Sorting the aggregated counts gives the trending hashtags.
Is Find Trending Hashtags asked at Google/Amazon/Meta?
Problems involving hashtag extraction and frequency counting are common variations of log processing and text analytics tasks seen in interviews at companies like Google, Amazon, and Meta. They test string parsing, hash-based aggregation, and SQL GROUP BY usage.
What data structure is used in Find Trending Hashtags?
A hash map (dictionary) is the core data structure for counting hashtag frequency. In SQL solutions, the equivalent concept is GROUP BY aggregation, which internally performs hash or sort-based grouping to compute counts for each hashtag.
What is the time complexity of Find Trending Hashtags?
The time complexity is O(n * m), where n is the number of tweets and m is the average length of each tweet. Each tweet must be scanned to locate hashtags, and every extracted hashtag updates a constant-time hash or grouping operation. Space complexity is O(k) for storing counts of unique hashtags.

Ready to solve this problem?

Practice Find Trending Hashtags with our built-in code editor and test cases.

Practice on FleetCode