-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
42 lines (39 loc) · 927 Bytes
/
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
using System;
using System.Collections.Generic;
namespace UniqueWordAbbreviation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
public class ValidWordAbbr
{
private Dictionary<string, HashSet<string>> dict;
public ValidWordAbbr(string[] dictionary)
{
dict = new Dictionary<string, HashSet<string>>();
foreach (var word in dictionary)
{
var abbr = GetAbbreviation(word);
if (!dict.ContainsKey(abbr))
dict[abbr] = new HashSet<string>();
dict[abbr].Add(word);
}
}
public bool IsUnique(string word)
{
var abbr = GetAbbreviation(word);
if (!dict.ContainsKey(abbr))
return true;
return dict[abbr].Count == 1 && dict[abbr].Contains(word);
}
private string GetAbbreviation(string word)
{
var count = word.Length - 2 <= 0 ? "" : (word.Length - 2).ToString();
return $"{word[0]}{count}{word[^1]}";
}
}
}