-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
67 lines (62 loc) · 2.26 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
namespace VowelSpellchecker
{
class Program
{
static void Main(string[] args)
{
}
public string[] Spellchecker(string[] wordlist, string[] queries)
{
HashSet<string> wordPerf = new HashSet<string>();
Dictionary<string, string> capError = new Dictionary<string, string>();
Dictionary<string, string> vowelError = new Dictionary<string, string>();
string[] result = new string[queries.Length];
foreach (string word in wordlist)
{
wordPerf.Add(word);
string lower = GetLower(word);
string vowelsRemoved = GetRemoveVowel(lower);
if (capError.ContainsKey(lower) == false)
capError[lower] = word;
if (vowelError.ContainsKey(vowelsRemoved) == false)
vowelError[vowelsRemoved] = word;
}
for (int i = 0; i < queries.Length; i++)
{
string qry = queries[i];
string lower = GetLower(qry);
string vRemoved = GetRemoveVowel(lower);
string output = string.Empty;
if (wordPerf.Contains(qry))
output = qry;
else if (capError.ContainsKey(lower))
output = capError[lower];
else if (vowelError.ContainsKey(vRemoved))
output = vowelError[vRemoved];
result[i] = output;
}
return result;
}
private string GetLower(string s)
{
char[] letters = s.ToCharArray();
for (int i = 0; i < letters.Length; i++)
letters[i] = char.ToLower(letters[i]);
return new string(letters);
}
private string GetRemoveVowel(string s)
{
char[] letters = s.ToCharArray();
for (int i = 0; i < letters.Length; i++)
if (IsVowel(letters[i]))
letters[i] = '*';
return new string(letters);
}
private bool IsVowel(char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
}
}