0
点赞
收藏
分享

微信扫一扫

LeetCode 819. 最常见的单词题解

yundejia 2022-04-17 阅读 61

819. 最常见的单词

题目来源:819. 最常见的单词

2022.04.17 每日一题

LeetCode 题解持续更新中GitHub仓库地址 CSDN博客地址

今天的题目较为简单,只需要根据题意简单的进行模拟即可

模拟 + HashMap

class Solution {
public:
    string mostCommonWord(string paragraph, vector<string> &banned) {
        // 创建列表 用于存储 banned 词汇
        set<string> set;

        for (string s: banned) {
            transform(s.begin(), s.end(), s.begin(), ::tolower);
            set.insert(s);
        }

        // 将 字符中的无效字符去除掉
        // 得到了一个只包含单词和空格的字符串
        char chars[]{'!', '?', '\'', ',', ';', '.'};
        for (char ch: chars)
            replace(paragraph.begin(), paragraph.end(), ch, ' ');
        // 将字符串按照 空格进行分割,得到了一个只包含 单词 和 空串 的字符串数组
        vector<string> split = split1(paragraph, " ");

        // 创建一个 HashMap ,用于统计各个单词的个数
        unordered_map<string, int> map;
        // 记录当前最大值
        int _max = 0;
        // 记录最大值对应的字符串
        string res = "";
        for (string s: split) {
            // 得到小写单词
            transform(s.begin(), s.end(), s.begin(), ::tolower);
            // 过滤掉禁止词汇 和 空串
            if (set.count(s) || s.length() == 0) continue;
            // 更新 最大值
            int num = map[s] + 1;
            map[s] = num;
            if (_max < num) {
                _max = num;
                res = s;
            }
        }
        // 返回要求
        return res;
    }

    vector<string> split1(const string &str, const string &pattern) {
        vector<string> res;
        if (str == "")
            return res;
        //在字符串末尾也加入分隔符,方便截取最后一段
        string strs = str + pattern;
        size_t pos = strs.find(pattern);

        while (pos != strs.npos) {
            string temp = strs.substr(0, pos);
            res.push_back(temp);
            //去掉已分割的字符串,在剩下的字符串中进行分割
            strs = strs.substr(pos + 1, strs.size());
            pos = strs.find(pattern);
        }

        return res;
    }
};
class Solution {
    public String mostCommonWord(String paragraph, String[] banned) {
        // 创建列表 用于存储 banned 词汇
        HashSet<String> set = new HashSet<>();
        for (String s : banned) set.add(s.toLowerCase());

        // 将 字符中的无效字符去除掉
        // 得到了一个只包含单词和空格的字符串
        char[] chars = {'!', '?', '\'', ',', ';', '.'};
        for (char ch : chars)
            paragraph = paragraph.replace(ch, ' ');
        // 将字符串按照 空格进行分割,得到了一个只包含 单词 和 空串 的字符串数组
        String[] split = paragraph.split(" ");

        // 创建一个 HashMap ,用于统计各个单词的个数
        HashMap<String, Integer> map = new HashMap<>();
        // 记录当前最大值
        int _max = 0;
        // 记录最大值对应的字符串
        String res = null;
        for (String s : split) {
            // 得到小写单词
            s = s.toLowerCase();
            // 过滤掉禁止词汇 和 空串
            if (set.contains(s) || s.length() == 0) continue;
            // 更新 最大值
            int num = map.getOrDefault(s, 0) + 1;
            map.put(s, num);
            if (_max < num) {
                _max = num;
                res = s;
            }
        }
        // 返回要求
        return res;
    }
}
举报

相关推荐

0 条评论