-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (71 loc) · 3 KB
/
Program.cs
File metadata and controls
93 lines (71 loc) · 3 KB
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
//Author: Anthony S. West - 2022/08/01 - Checks two strings and compares for an anagram (fast, well, for C# anyway lol)
using System;
//net 6 is really screwy with console apps not needing an explicit main function - bleh - doing it the old way
// See https://aka.ms/new-console-template for more information
//Console.WriteLine("Hello, World!");
namespace TestAnagram
{
internal class Program
{
// ----------------------------------------------------------------------------------------
static int Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Must have two args, both strings.");
return 1;
}
string str1 = args[0], str2 = args[1];
Console.WriteLine($"1: \"{str1}\", 2: \"{str2}\"");
bool isAnagram = IsAnagram(str1, str2);
Console.WriteLine($"Is anagram? {isAnagram}");
//Console.WriteLine("Press any key to continue...");
//Console.ReadKey();
return 0;
}
// ----------------------------------------------------------------------------------------
static bool IsAnagram(string str1, string str2)
{
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
return false;
str1 = str1.ToLower();
str2 = str2.ToLower();
if (str1 == str2)
return true;
const int nAlphaChars = 26;
int[] str1AlphaCounts = new int[nAlphaChars];
int[] str2AlphaCounts = new int[nAlphaChars];
//build char counts for string 1 for only alpha letters
foreach (char ch in str1)
{
int idx = ch - 'a';
if (idx >= 0 && idx < nAlphaChars)
str1AlphaCounts[idx]++;
}
//build char counts for string 2 for only alpha letters
foreach (char ch in str2)
{
int idx = ch - 'a';
if (idx >= 0 && idx < nAlphaChars)
str2AlphaCounts[idx]++;
}
return CountsTheSame(str1AlphaCounts, str2AlphaCounts);
}
// ----------------------------------------------------------------------------------------
private static bool CountsTheSame(int[] str1AlphaCounts, int[] str2AlphaCounts)
{
if (null == str1AlphaCounts || null == str2AlphaCounts)
return false;
if (str1AlphaCounts.Length != str2AlphaCounts.Length)
return false;
for (int i = 0; i < str1AlphaCounts.Length; i++)
{
if (str1AlphaCounts[i] != str2AlphaCounts[i])
return false;
}
return true;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}