public class Solution
    {
        public bool IsLongPressedName(string name, string typed)
        {
            var list1 = new List<KeyValuePair<char, int>>();
            var list2 = new List<KeyValuePair<char, int>>();

            int name_len = name.Length;
            int typed_len = typed.Length;
            if (name_len > typed_len)
            {
                return false;
            }
            int con = 1;
            var last_char = ' ';
            for (int i = 0; i < name_len - 1; i++)
            {
                var cur_char = name[i];
                var next_char = name[i + 1];
                last_char = next_char;
                if (cur_char == next_char)
                {
                    con++;
                }
                else
                {
                    list1.Add(new KeyValuePair<char, int>(cur_char, con));
                    con = 1;
                }
            }
            list1.Add(new KeyValuePair<char, int>(last_char, con));


            con = 1;
            last_char = ' ';
            for (int i = 0; i < typed_len - 1; i++)
            {
                var cur_char = typed[i];
                var next_char = typed[i + 1];
                last_char = next_char;
                if (cur_char == next_char)
                {
                    con++;
                }
                else
                {
                    list2.Add(new KeyValuePair<char, int>(cur_char, con));
                    con = 1;
                }
            }
            list2.Add(new KeyValuePair<char, int>(last_char, con));
            if (list1.Count > list2.Count)
            {
                return false;
            }
            for (int i = 0; i < list1.Count; i++)
            {
                if (list1[i].Key != list2[i].Key || list1[i].Value > list2[i].Value)
                {
                    return false;
                }
            }
            return true;
        }
    }

 

posted on 2018-10-21 17:36  Sempron2800+  阅读(110)  评论(0编辑  收藏  举报