Skip to content

Instantly share code, notes, and snippets.

@lucas-zimerman
Last active April 17, 2021 20:58
Show Gist options
  • Save lucas-zimerman/f43a7d2243b173a6e35078b10214a861 to your computer and use it in GitHub Desktop.
Save lucas-zimerman/f43a7d2243b173a6e35078b10214a861 to your computer and use it in GitHub Desktop.
Find Anagram C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace AnagramFinder
{
public static class Program
{
public static void Main(string[] args)
{
var rand = new Random();
var data = new string[] { "petter", "cat", "tab", "in", "state", "taste", "act", "at", "bat", "the", "tub", "dog", "storage", "node", "C#", "not", "is", "park", "spider", "man", "dare", "dear", "for", "young", "light", "basiparachromatin", "marsipobranchiata", "conversationalists", "conservationalists", "hydroxydeoxycorticosterones", "hydroxydesoxycorticosterone", "basiparachromatin", "marsipobranchiata" };
var testList = new List<string> {
"cat in the bat",
"petter park is the spider man",
"dog, cat tab act",
"petter taste for car tub is not for young state",
"act in the tub",
"cat is not bat",
"C# is not node storage",
"bat in the storage is not cat",
"cat is bat in the dog",
"dear petter is the man" };
for (int ii = 0; ii < 1_000_000; ii++)
{
testList.Add("marsipobranchiata basiparachromatin hydroxydeoxycorticosterones conversationalists");
testList.Add("in the state at conservationalists the dog spider taste in the park");
}
var stopWatch = new Stopwatch();
stopWatch.Start();
int totalAnagrams = 0;
int totalNotAnagrams = 0;
int end = data.Length;
int halfEnd = 1 + (data.Length >> 1);
var anagrams = new string[data.Length];
var notAnagrams = new string[data.Length];
var skipItems = new bool[data.Length];
bool notFound;
int i = 0, j = 0;
//find anagrams
for (; i < end; i++)
{
if (!skipItems[i])
{
notFound = true;
for (j = i + 1; j < end; j++)
{
if (!skipItems[j] && data[i].IsAnagram(data[j]))
{
anagrams[totalAnagrams++] = data[i];
skipItems[j] = true;
notFound = false;
j = data.Length;
}
}
if (notFound)
{
notAnagrams[totalNotAnagrams++] = data[i];
}
}
}
var anagramCount = new int[testList.Count, 2];
var possibleCombinations = new int[testList.Count];
string test;
//track options
for (i = 0; i < testList.Count; i++)
{
anagramCount[i, 0] = 0;
anagramCount[i, 1] = 0;
end = testList[i].Length;
//find words
j = 0;
while (j < end)
{
if (testList[i].IndexStartWithNonAnagram(ref j, notAnagrams, totalNotAnagrams))
{
anagramCount[i, 1]++;
}
else
{
anagramCount[i, 0]++;
}
//Move cursor to the next word
j = testList[i].IndexOf(' ', j);
if(j == -1) { j = end; }
//skip the space
j++;
}
possibleCombinations[i] = 1 << anagramCount[i, 0];
}
stopWatch.Stop();
Console.WriteLine("Created By: Lucas Zimerman");
Console.WriteLine("Input WORDS");
foreach (var text in data)
{
Console.Write($"{text}, ");
i++;
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Input Texts");
/*
foreach (var text in testList)
{
Console.WriteLine($"Word: {text}");
i++;
}
*/
Console.WriteLine(
$@"
_______
/ 12 \
| | | Elapsed time
|9 | 3| {stopWatch.ElapsedMilliseconds} ms {stopWatch.ElapsedTicks} ticks
| \ |
\___6___/
");
Console.WriteLine($"Total Anagrams: {totalAnagrams}");
Console.WriteLine($"Total not Anagrams: {totalNotAnagrams}\n");
Console.WriteLine("Possible Combinations:");
i = 0;
/*
foreach (var text in testList)
{
Console.WriteLine($"Word: ({text}) Combinations: {possibleCombinations[i]} Total: {anagramCount[i, 0]} Anagrams {anagramCount[i, 1]} Others");
i++;
}
*/
Console.WriteLine("\n\nPress any key to exit");
Console.ReadKey();
}
public static bool IsAnagram(this string source, string target)
{
if (source.Length != target.Length) return false;
int sum = 0;
for (int index = 0; index < source.Length; index++)
{
sum += source[index] - target[index];
}
return sum == 0;
}
public static bool IndexStartWithNonAnagram(this string source, ref int index, string[] nonAnagrams, int nonAnagramsCount)
{
//END for true:
// reached the space with a matching non anagram
//FALSE otherwise
int currentIndex = index;
int LastCheckedEndIndex = 0;
for (int i = 0; i < nonAnagramsCount; i++)
{
//check if the word on the given index matches the nonAnagram i1000
for (int j = 0;
j < nonAnagrams[i].Length && currentIndex < source.Length && nonAnagrams[i][j] == source[currentIndex];
j++, currentIndex++) ;
//If we reached the end of the word, we found a non anagram word
if (currentIndex == source.Length || source[currentIndex] == ' ')
{
index = currentIndex;
return true;
}
else if (nonAnagrams[i].Length > LastCheckedEndIndex)
{
LastCheckedEndIndex = nonAnagrams[i].Length;
}
currentIndex = index;
}
index = currentIndex;
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment