20220417力扣每日一题

问题描述

819. 最常见的单词

给定一个段落 (paragraph) 和一个禁用单词列表 (banned)。返回出现次数最多,同时不在禁用列表中的单词。

题目保证至少有一个词不在禁用列表中,而且答案唯一

禁用列表中的单词用小写字母表示,不含标点符号。段落中的单词不区分大小写。答案都是小写字母

示例:

输入: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
输出: "ball"
解释: 
"hit" 出现了3次,但它是一个禁用的单词。
"ball" 出现了2次 (同时没有其他单词出现2次),所以它是段落里出现次数最多的,且不在禁用列表中的单词。 
注意,所有这些单词在段落里不区分大小写,标点符号需要忽略(即使是紧挨着单词也忽略, 比如 "ball,"), 
"hit"不是最终的答案,虽然它出现次数更多,但它在禁用单词列表中。

提示:

1 <= 段落长度 <= 1000
0 <= 禁用单词个数 <= 100
1 <= 禁用单词长度 <= 10
答案是唯一的, 且都是小写字母 (即使在 paragraph 里是大写的,即使是一些特定的名词,答案都是小写的。)
paragraph 只包含字母、空格和下列标点符号!?',;.
不存在没有连字符或者带有连字符的单词。
单词里只包含字母,不会出现省略号或者其他标点符号。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/most-common-word
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

基本想法

        要获得目标答案,至少需要遍历一遍整个段落,识别单词的规则是:在遍历过程中暂存一个空串s,只要碰到标点符号(,?';)和空格('\32')时,单个单词识别结束,如果碰到非标点符号,则加入s。如果此单个单词识别结束时,s非空,则将s保存进一个哈希表dict中,dict的Key Value就是s的内容,而Mapped Value则为s出现的频度,s加入后,频度自动+1。需要注意的是,如果s出现在banned中,则忽略此次加入的s

        同时每次执行完s加入dict后,维护当前的最大可出现单词频度变量max,同时记录此时max对应的目标单词res,遍历结束时,res即为所求结果。

伪代码过程

range <- paragraph_size; 
x <- 0; 'index of paragraph
dict <- null hashmap;
max <- 0;
res <- null string;
while(x < range):
    s <- null string;
    while((x < range) and (paragraph[x] is not a (Notation or Space))):
        s appends paragraph[x];
        x <- x + 1;
    end while;
    if((s is not a null string) and (s is not in banned_set)):
        s_in_dict_frequency <- s_in_dict_frequency + 1;
        if(s_in_dict_frequency > max):
            max <- s_in_dict_frequency;
            res <- s;
        end if;
    end if;
    x <- x + 1;
end while;
return res;

完整代码(C++)

class Solution {
public:
    bool isNotation(char& c) {
        return c == ' ' || c == ',' || c == '.' || c == '!' || c == '?' || c == '\'' || c == ';';
    }
    string mostCommonWord(string paragraph, vector<string>& banned) {
        unordered_map<string, int> dict;
        unordered_set<string> ban;
        string res = "";
        if(!banned.empty()) {
            for(auto& a : banned) {
                ban.insert(a);
            }
        }
        int index = 0;
        int N = paragraph.size();
        int max = 0;
        while(index < N) {
            string s = "";
            while(index < N && !isNotation(paragraph[index])) {
                if(paragraph[index] >= 'A' && paragraph[index] <= 'Z')
                    paragraph[index] += 32;
                s.push_back(paragraph[index]);
                index++;
            }
            if(s.size() == 0) {
                index++;
                continue;
            }
            if(ban.empty() || !ban.count(s)) {
                dict[s]++;
                if(dict[s] > max) {
                    max = dict[s];
                    res = s;
                }
            }
            index++;
        }
        return res;
    }
};

最终结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值