-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagrams
45 lines (34 loc) · 1.41 KB
/
Anagrams
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
namespace TGS.Challenge
{
/*
Devise a function that checks if 1 word is an anagram of another, if the words are anagrams of
one another return true, else return false
"Anagram": An anagram is a type of word play, the result of rearranging the letters of a word or
phrase to produce a new word or phrase, using all the original letters exactly once; for example
orchestra can be rearranged into carthorse.
areAnagrams("horse", "shore") should return true
areAnagrams("horse", "short") should return false
NOTE: Punctuation, including spaces should be ignored, e.g.
horse!! shore = true
horse !! shore = true
horse? heroes = true
There are accompanying unit tests for this exercise, ensure all tests pass & make
sure the unit tests are correct too.
*/
public class Anagram
{
public bool AreAnagrams(string word1, string word2)
{
bool returnResult = false;
string CleanWord(string word)
{
return new string(word.Where(char.IsLetterOrDigit).ToArray());
}
string cleanWord1 = CleanWord(word1);
string cleanWord2 = CleanWord(word2);
returnResult = string.Concat(cleanWord1.ToLower().OrderBy(c => c)) ==
string.Concat(cleanWord2.ToLower().OrderBy(c => c));
return returnResult;
}
}
}