zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    11111
    JavaScript基础
    CSS使用
    CSS 基本
    前端之HTML
    Python 迭代器和生成器
    Python day11
    Python 函数
    第十七篇:django基础(二)
    第十六篇:django基础
  • 原文地址:https://www.cnblogs.com/asenyang/p/9733635.html
Copyright © 2011-2022 走看看