|
| 1 | +# [3150. Invalid Tweets II 🔒](https://leetcode.com/problems/invalid-tweets-ii) |
| 2 | + |
| 3 | +[中文文档](/solution/3100-3199/3150.Invalid%20Tweets%20II/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 | +| tweet_id | int | |
| 16 | +| content | varchar | |
| 17 | ++----------------+---------+ |
| 18 | +tweet_id is the primary key (column with unique values) for this table. |
| 19 | +This table contains all the tweets in a social media app. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p>Write a solution to find <strong>invalid tweets</strong>. A tweet is considered invalid if it meets <strong>any</strong> of the following criteria:</p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li>It exceeds <code>140</code> characters in length.</li> |
| 26 | + <li>It has more than <code>3</code> mentions.</li> |
| 27 | + <li>It includes more than <code><font face="monospace">3</font></code> hashtags.</li> |
| 28 | +</ul> |
| 29 | + |
| 30 | +<p>Return <em>the result table ordered by</em> <code>tweet_id</code> <em>in <strong>ascending</strong> order</em>.</p> |
| 31 | + |
| 32 | +<p>The result format is in the following example.</p> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Example:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong></p> |
| 39 | + |
| 40 | +<p>Tweets table:</p> |
| 41 | + |
| 42 | +<pre class="example-io"> |
| 43 | + +----------+-----------------------------------------------------------------------------------+ |
| 44 | + | tweet_id | content | |
| 45 | + +----------+-----------------------------------------------------------------------------------+ |
| 46 | + | 1 | Traveling, exploring, and living my best life @JaneSmith @SaraJohnson @LisaTaylor | |
| 47 | + | | @MikeBrown #Foodie #Fitness #Learning | |
| 48 | + | 2 | Just had the best dinner with friends! #Foodie #Friends #Fun | |
| 49 | + | 4 | Working hard on my new project #Work #Goals #Productivity #Fun | |
| 50 | + +----------+-----------------------------------------------------------------------------------+ |
| 51 | + </pre> |
| 52 | + |
| 53 | +<p><strong>Output:</strong></p> |
| 54 | + |
| 55 | +<pre class="example-io"> |
| 56 | + +----------+ |
| 57 | + | tweet_id | |
| 58 | + +----------+ |
| 59 | + | 1 | |
| 60 | + | 4 | |
| 61 | + +----------+ |
| 62 | + </pre> |
| 63 | + |
| 64 | +<p><strong>Explanation:</strong></p> |
| 65 | + |
| 66 | +<ul> |
| 67 | + <li>tweet_id 1 contains 4 mentions.</li> |
| 68 | + <li>tweet_id 4 contains 4 hashtags.</li> |
| 69 | +</ul> |
| 70 | +Output table is ordered by tweet_id in ascending order.</div> |
| 71 | + |
| 72 | +## Solutions |
| 73 | + |
| 74 | +### Solution 1: LENGTH() Function + REPLACE() Function |
| 75 | + |
| 76 | +We can use the `LENGTH()` function to calculate the length of the string, calculate the length after excluding `@` or `#`, then use the `OR` operator to connect these three conditions, filter out the corresponding tweet_id, and sort by tweet_id in ascending order. |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +```sql |
| 81 | +# Write your MySQL query statement below |
| 82 | +SELECT tweet_id |
| 83 | +FROM Tweets |
| 84 | +WHERE LENGTH(content) > 140 |
| 85 | + OR (LENGTH(content) - LENGTH(REPLACE(content, '@', ''))) > 3 |
| 86 | + OR (LENGTH(content) - LENGTH(REPLACE(content, '#', ''))) > 3 |
| 87 | +ORDER BY 1; |
| 88 | +``` |
| 89 | + |
| 90 | +```python |
| 91 | +import pandas as pd |
| 92 | + |
| 93 | + |
| 94 | +def find_invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame: |
| 95 | + invalid_tweets = tweets[ |
| 96 | + (tweets["content"].str.len() > 140) |
| 97 | + | (tweets["content"].str.count("@") > 3) |
| 98 | + | (tweets["content"].str.count("#") > 3) |
| 99 | + ].sort_values(by="tweet_id") |
| 100 | + return invalid_tweets[["tweet_id"]] |
| 101 | +``` |
| 102 | + |
| 103 | +<!-- tabs:end --> |
| 104 | + |
| 105 | +<!-- end --> |
0 commit comments