leetcode925

时间:2023-03-09 17:18:08
leetcode925
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 = ;
var last_char = ' ';
for (int i = ; i < name_len - ; i++)
{
var cur_char = name[i];
var next_char = name[i + ];
last_char = next_char;
if (cur_char == next_char)
{
con++;
}
else
{
list1.Add(new KeyValuePair<char, int>(cur_char, con));
con = ;
}
}
list1.Add(new KeyValuePair<char, int>(last_char, con)); con = ;
last_char = ' ';
for (int i = ; i < typed_len - ; i++)
{
var cur_char = typed[i];
var next_char = typed[i + ];
last_char = next_char;
if (cur_char == next_char)
{
con++;
}
else
{
list2.Add(new KeyValuePair<char, int>(cur_char, con));
con = ;
}
}
list2.Add(new KeyValuePair<char, int>(last_char, con));
if (list1.Count > list2.Count)
{
return false;
}
for (int i = ; i < list1.Count; i++)
{
if (list1[i].Key != list2[i].Key || list1[i].Value > list2[i].Value)
{
return false;
}
}
return true;
}
}