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;
        }
    }

 

posted on 2018-09-30 22:50  Sempron2800+  阅读(118)  评论(0编辑  收藏  举报