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

 

转载于:https://www.cnblogs.com/asenyang/p/9733635.html

你可能感兴趣的:(leetcode819)