返回介绍

solution / 2700-2799 / 2738.Count Occurrences in Text / README_EN

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

2738. Count Occurrences in Text

中文文档

Description

Table: Files

+-------------+---------+
| Column Name | Type  |
+-- ----------+---------+
| file_name   | varchar |
| content   | text  |
+-------------+---------+
file_name is the column with unique values of this table. 
Each row contains file_name and the content of that file.

Write a solution to find the number of files that have at least one occurrence of the words 'bull' and 'bear' as a standalone word, respectively, disregarding any instances where it appears without space on either side (e.g. 'bullet', 'bears', 'bull.', or 'bear' at the beginning or end of a sentence will not be considered) 

Return _the word 'bull' and 'bear' along with the corresponding number of occurrences in any order._

The result format is in the following example.

 

Example 1:

Input: 
Files table:
+------------+----------------------------------------------------------------------------------+
| file_name  | content                                     | 
+------------+----------------------------------------------------------------------------------+
| draft1.txt | The stock exchange predicts a bull market which would make many investors happy. | 
| draft2.txt | The stock exchange predicts a bull market which would make many investors happy, |
|        | but analysts warn of possibility of too much optimism and that in fact we are  |
|        | awaiting a bear market.                              | 
| draft3.txt | The stock exchange predicts a bull market which would make many investors happy, |
|        | but analysts warn of possibility of too much optimism and that in fact we are  |
|        | awaiting a bear market. As always predicting the future market is an uncertain   |
|      | game and all investors should follow their instincts and best practices.     | 
+------------+----------------------------------------------------------------------------------+
Output: 
+------+-------+
| word | count |  
+------+-------+
| bull | 3   | 
| bear | 2   | 
+------+-------+
Explanation: 
- The word "bull" appears 1 time in "draft1.txt", 1 time in "draft2.txt", and 1 time in "draft3.txt". Therefore, the total number of occurrences for the word "bull" is 3.
- The word "bear" appears 1 time in "draft2.txt", and 1 time in "draft3.txt". Therefore, the total number of occurrences for the word "bear" is 2.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT 'bull' AS word, COUNT(*) AS count
FROM Files
WHERE content LIKE '% bull %'
UNION
SELECT 'bear' AS word, COUNT(*) AS count
FROM Files
WHERE content LIKE '% bear %';

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

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

发布评论

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