819. Most Common Word

该博客介绍了一个简单的字符串处理问题,要求从段落中找出出现频率最高且不在禁用词列表中的单词。虽然题目看似简单,但在处理时需要注意一些细节,例如对大小写的忽略、符号的剔除等。提供的代码示例中,通过预处理去除非字母字符,并使用`unordered_map`记录单词频率,成功解决了问题。然而,案例中的特殊情况可能导致错误结果,需要额外处理连续的禁用词边界情况。
摘要由CSDN通过智能技术生成

题目:

Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

The words in paragraph are case-insensitive and the answer should be returned in lowercase.

Example 1:

Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.

Example 2:

Input: paragraph = "a.", banned = []
Output: "a"

Constraints:

  • 1 <= paragraph.length <= 1000
  • paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
  • 0 <= banned.length <= 100
  • 1 <= banned[i].length <= 10
  • banned[i] consists of only lowercase English letters.

思路:

通过率只有40+的简单题,读了一下发现没难度,结果做完发现是第46个case有那个大病。"a, a, a, a, b,b,b,c, c" ["a"],仔细看,b后面是没有空格的,如果用stringsstream读进来,会导致"b,b,b,c"是一个单词。不知道是不是出题人笔误,但是把它放在倒数几个case感觉是故意恶心人加的,导致通过率极低。解决方法就是在预处理时先将非字母的剔除即可。原本如果没有这个case,string大写转小写用transform(begin(s), end(s), begin(s), ::tolower)即可,但是这里要剔除符号,只能手动遍历了。

代码:

class Solution {
public:
    string mostCommonWord(string paragraph, vector<string>& banned) {
        unordered_set<string> ban(begin(banned), end(banned));
        unordered_map<string, int> record;
        int count = 0;
        string ans;
        for (auto & c: paragraph) c = isalpha(c) ? tolower(c) : ' ';
        istringstream ss(paragraph);
        string tmp;
        while (ss >> tmp) {
            if (ban.count(tmp))
                continue;
            record[tmp]++;
            if (record[tmp] > count) {
                count = record[tmp];
                ans = tmp;
            }
        }   
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值