返回介绍

solution / 2100-2199 / 2199.Finding the Topic of Each Post / README_EN

发布于 2024-06-17 01:03:08 字数 4591 浏览 0 评论 0 收藏 0

2199. Finding the Topic of Each Post

中文文档

Description

Table: Keywords

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| topic_id  | int   |
| word    | varchar |
+-------------+---------+
(topic_id, word) is the primary key (combination of columns with unique values) for this table.
Each row of this table contains the id of a topic and a word that is used to express this topic.
There may be more than one word to express the same topic and one word may be used to express multiple topics.

 

Table: Posts

+-------------+---------+
| Column Name | Type  |
+-------------+---------+
| post_id   | int   |
| content   | varchar |
+-------------+---------+
post_id is the primary key (column with unique values) for this table.
Each row of this table contains the ID of a post and its content.
Content will consist only of English letters and spaces.

 

Leetcode has collected some posts from its social media website and is interested in finding the topics of each post. Each topic can be expressed by one or more keywords. If a keyword of a certain topic exists in the content of a post (case insensitive) then the post has this topic.

Write a solution to find the topics of each post according to the following rules:

  • If the post does not have keywords from any topic, its topic should be "Ambiguous!".
  • If the post has at least one keyword of any topic, its topic should be a string of the IDs of its topics sorted in ascending order and separated by commas ','. The string should not contain duplicate IDs.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Keywords table:
+----------+----------+
| topic_id | word   |
+----------+----------+
| 1    | handball |
| 1    | football |
| 3    | WAR    |
| 2    | Vaccine  |
+----------+----------+
Posts table:
+---------+------------------------------------------------------------------------+
| post_id | content                                |
+---------+------------------------------------------------------------------------+
| 1     | We call it soccer They call it football hahaha             |
| 2     | Americans prefer basketball while Europeans love handball and football |
| 3     | stop the war and play handball                     |
| 4     | warning I planted some flowers this morning and then got vaccinated  |
+---------+------------------------------------------------------------------------+
Output: 
+---------+------------+
| post_id | topic    |
+---------+------------+
| 1     | 1      |
| 2     | 1      |
| 3     | 1,3    |
| 4     | Ambiguous! |
+---------+------------+
Explanation: 
1: "We call it soccer They call it football hahaha"
"football" expresses topic 1. There is no other word that expresses any other topic.

2: "Americans prefer basketball while Europeans love handball and football"
"handball" expresses topic 1. "football" expresses topic 1. 
There is no other word that expresses any other topic.

3: "stop the war and play handball"
"war" expresses topic 3. "handball" expresses topic 1.
There is no other word that expresses any other topic.

4: "warning I planted some flowers this morning and then got vaccinated"
There is no word in this sentence that expresses any topic. Note that "warning" is different from "war" although they have a common prefix. 
This post is ambiguous.

Note that it is okay to have one word that expresses more than one topic.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  post_id,
  IFNULL(GROUP_CONCAT(DISTINCT topic_id), 'Ambiguous!') AS topic
FROM
  Posts
  LEFT JOIN Keywords ON INSTR(CONCAT(' ', content, ' '), CONCAT(' ', word, ' ')) > 0
GROUP BY post_id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文