首页 > 其他分享 >leetcode-819-easy

leetcode-819-easy

时间:2022-11-08 19:59:09浏览次数:105  
标签:hit String banned 819 paragraph easy word words leetcode

Most Common Word

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.

思路一:分割字符串,然后统计。由于用了正则,效率会差很多,自己实现分割算法效率会好很多。

public static String mostCommonWord(String paragraph, String[] banned) {

    Map<String, Integer> map = new HashMap<>();
    String[] arr = paragraph.split("[!?',;.\\s]");
    for (String s : arr) {
        if (s.isEmpty()) continue;
        map.compute(s.toLowerCase(), (k, v) -> v == null ? 1 : v + 1);
    }

    for (String s : banned) {
        map.remove(s);
    }

    int max = 0;
    String val = "";
    for (Map.Entry<String, Integer> e : map.entrySet()) {
        if (e.getValue() > max) {
            max = e.getValue();
            val = e.getKey();
        }
    }

    return val;
}

标签:hit,String,banned,819,paragraph,easy,word,words,leetcode
From: https://www.cnblogs.com/iyiluo/p/16870946.html

相关文章

  • LeetCode 435. Non-overlapping Intervals
    贪心按照有边界排序,只有先选择了右边边界小的,才可以放下更多的区间classSolution{public:interaseOverlapIntervals(vector<vector<int>>&intervals){......
  • leetcode-1260-easy
    Shift2DGridGivena2Dgridofsizemxnandanintegerk.Youneedtoshiftthegridktimes.Inoneshiftoperation:Elementatgrid[i][j]movestogrid......
  • leetcode-561-easy
    ArrayPartitionGivenanintegerarraynumsof2nintegers,grouptheseintegersintonpairs(a1,b1),(a2,b2),...,(an,bn)suchthatthesumofmin(ai,bi......
  • leetcode-1854-easy
    MaximumPopulationYearYouaregivena2Dintegerarraylogswhereeachlogs[i]=[birthi,deathi]indicatesthebirthanddeathyearsoftheithperson.The......
  • leetcode-1684-easy
    CounttheNumberofConsistentStringsYouaregivenastringallowedconsistingofdistinctcharactersandanarrayofstringswords.Astringisconsistenti......
  • LeetCode 135. Candy
    贪心算法贪心策略:在每次遍历中,仅考虑并更新相邻一侧的大小关系classSolution{public:intcandy(vector<int>&ratings){intsize=ratings.size();......
  • LeetCode 455. Assign Cookies
    贪心classSolution{public:intfindContentChildren(vector<int>&g,vector<int>&s){sort(g.begin(),g.end());sort(s.begin(),s.end());......
  • Leetcode练题系列(六): 字符串相关的算法
    LeetCode  ​​英文官网(推荐)​​  ​​中文官网​​  从2016年大二左右开始就接触算法,起初也简单练习过,但现在工作一段时间后,随着代码水平的提高(​​自我感觉​​)......
  • 视频融合平台EasyCVR首路录像无法播放是什么原因?该如何解决?
    EasyCVR具备较强的视频能力,可支持海量设备接入、汇聚与管理、视频监控、视频录像、云存储、回放与检索、智能告警、平台级联等功能。平台可支持多协议接入,包括:国标GB/T2818......
  • 视频融合平台EasyCVR继承播放器,但是无法播放该如何解决?
    EasyCVR视频融合云平台基于云边端一体化架构,兼容性高、拓展性强,可支持多类型设备、多协议方式接入,包括国标GB/T28181、RTMP、RTSP/Onvif协议,以及厂家的私有协议,如:海康Ehome......