public class Solution
    {
        public string ShortestCompletingWord(string licensePlate, string[] words)
        {
            var list = words.OrderBy(x => x.Length);
            var pattern = licensePlate.ToLower();
            var dic = new Dictionary<char, int>();
            foreach (var p in pattern)
            {
                if (p >= 97 && p <= 122)
                {
                    if (!dic.ContainsKey(p))
                    {
                        dic.Add(p, 1);
                    }
                    else
                    {
                        dic[p]++;
                    }
                }
            }

            foreach (var word in list)
            {
                bool find = true;
                var dic2 = new Dictionary<char, int>();
                foreach (var p in word)
                {
                    if (!dic2.ContainsKey(p))
                    {
                        dic2.Add(p, 1);
                    }
                    else
                    {
                        dic2[p]++;
                    }
                }

                foreach (var d in dic)
                {
                    if (!dic2.ContainsKey(d.Key) || dic2[d.Key] < dic[d.Key])
                    {
                        find = false;
                        break;
                    }
                }
                if (find)
                {
                    return word;
                }
            }

            return "";
        }
    }

 

posted on 2018-10-21 18:56  Sempron2800+  阅读(103)  评论(0编辑  收藏  举报