-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anagram.cs
96 lines (85 loc) · 4.51 KB
/
Anagram.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Globalization;
namespace AnagramTask
{
public class Anagram
{
/// <summary>
/// Initializes a new instance of the <see cref="Anagram"/> class.
/// </summary>
/// <param name="sourceWord">Source word.</param>
/// <exception cref="ArgumentNullException">Thrown when source word is null.</exception>
/// <exception cref="ArgumentException">Thrown when source word is empty.</exception>
private readonly string txt;
public Anagram(string sourceWord)
{
if (sourceWord == null)
{
throw new ArgumentNullException(nameof(sourceWord));
}
if (string.IsNullOrEmpty(sourceWord))
{
throw new ArgumentException("source word is empty", nameof(sourceWord));
}
this.txt = sourceWord;
}
/// <summary>
/// From the list of possible anagrams selects the correct subset.
/// </summary>
/// <param name="candidates">A list of possible anagrams.</param>
/// <returns>The correct sublist of anagrams.</returns>
/// <exception cref="ArgumentNullException">Thrown when candidates list is null.</exception>
public string[] FindAnagrams(string[] candidates)
{
if (candidates == null)
{
throw new ArgumentNullException(nameof(candidates));
}
string[] fakeresultstring = new string[3];
int countofmatches = 0;
int fakearraylenght = 0;
for (int arraySnumber = 0; arraySnumber < candidates.Length; arraySnumber++)
{
for (int symbloofarray = 0; symbloofarray < candidates[arraySnumber].Length; symbloofarray++)
{
for (int fieldSchar = 0; fieldSchar < this.txt.Length; fieldSchar++)
{
// длинный if это сравнение чтобы индексы первого вхождние и последнего не совпадали, ибо тогда буквы повторяются и мы break и
// проверка на -1, ибо если -1 то в строке нету такого, извините за грязь, если буду успевать - переделаю
if (candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture)[symbloofarray] == this.txt.ToUpper(CultureInfo.CurrentCulture)[fieldSchar])
{
countofmatches++;
if (candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture).IndexOf(this.txt.ToUpper(CultureInfo.CurrentCulture)[fieldSchar], StringComparison.CurrentCulture) != -1 && candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture).LastIndexOf(this.txt.ToUpper(CultureInfo.CurrentCulture)[fieldSchar]) != -1 && (candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture).IndexOf(this.txt.ToUpper(CultureInfo.CurrentCulture)[fieldSchar], StringComparison.CurrentCulture) != candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture).LastIndexOf(this.txt.ToUpper(CultureInfo.CurrentCulture)[fieldSchar])))
{
break;
}
}
}
// Words_Are_Not_Anagrams_Of_Themselves
if (candidates[arraySnumber].ToUpper(CultureInfo.CurrentCulture) == this.txt.ToUpper(CultureInfo.CurrentCulture))
{
break;
}
}
if (countofmatches == candidates[arraySnumber].Length && candidates[arraySnumber].Length == this.txt.Length)
{
fakeresultstring[fakearraylenght] = candidates[arraySnumber];
fakearraylenght++;
}
countofmatches = 0;
}
// это на проверку пустоты fakeresultstring чтобы вернуть в конце чистый массив без string.empty
int arraylenght = 0;
for (int i = 0; i < fakeresultstring.Length; i++)
{
if (!string.IsNullOrEmpty(fakeresultstring[i]))
{
arraylenght++;
}
}
string[] resultstring = fakeresultstring[0..arraylenght];
return resultstring;
}
}
}