leetcode819

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode819相关的知识,希望对你有一定的参考价值。

public class Solution
    {
        public string MostCommonWord(string paragraph, string[] banned)
        {
            //"a, a, a, a, b,b,b,c, c"
            paragraph = paragraph.ToLower().Replace("!", " ").Replace("?", " ").Replace("", " ").Replace(",", " ").Replace(";", " ").Replace(".", " ");
            var list = paragraph.Split( );
            var dic = new Dictionary<string, int>();
            foreach (var l in list)
            {
                var temp = l.Trim();
                if (temp.Length == 0)
                {
                    continue;
                }
                var filter = banned.Where(x => x == temp).ToList();
                if (filter.Count > 0)
                {
                    continue;
                }
                if (!dic.ContainsKey(temp))
                {
                    dic.Add(temp, 1);
                }
                else
                {
                    dic[temp]++;
                }
            }

            var result = dic.OrderByDescending(x => x.Value).ToList().FirstOrDefault();
            return result.Key;
        }
    }

 

以上是关于leetcode819的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-819-Most Common Word(词频统计)

[LeetCode] 819. Most Common Word

LeetCode 819. 最常见的单词

LeetCode 819 最常见的单词[Map 模拟] HERODING的LeetCode之路

Java n种方式分割统计单词

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段