-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
49 lines (48 loc) · 1.45 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
namespace ValidWordAbbreviation
{
class Program
{
static void Main(string[] args)
{
string word = "a", abbr = "01";
Console.WriteLine(ValidWordAbbreviation(word, abbr));
}
static bool ValidWordAbbreviation(string word, string abbr)
{
var dict = new Dictionary<char, List<int>>();
string num = "";
int index = -1;
foreach (var character in abbr)
{
if (char.IsLetter(character))
{
if (num.StartsWith('0'))
return false;
index += num == "" ? 1 : int.Parse(num) + 1;
if (!dict.ContainsKey(character))
dict[character] = new List<int>();
dict[character].Add(index);
num = "";
}
else
num += character;
}
if (num.StartsWith('0'))
return false;
index += num == "" ? 1 : int.Parse(num) + 1;
if (index != word.Length)
return false;
foreach (var kv in dict)
{
foreach (var i in kv.Value)
{
if (word[i] != kv.Key)
return false;
}
}
return true;
}
}
}