码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode819

时间:2018-09-30 23:19:21      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:replace   lse   pre   result   where   span   style   else   etc   

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

标签:replace   lse   pre   result   where   span   style   else   etc   

原文地址:https://www.cnblogs.com/asenyang/p/9733635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!