Skip to content

Instantly share code, notes, and snippets.

@Xmerr
Created December 7, 2016 13:56
Show Gist options
  • Save Xmerr/f6b09f83b4dccabaffea49b0de80a496 to your computer and use it in GitHub Desktop.
Save Xmerr/f6b09f83b4dccabaffea49b0de80a496 to your computer and use it in GitHub Desktop.
For Reddit Intermediate Challenge 280
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Anagram_Maker
{
class Program
{
static string remainingLetters;
static void Main(string[] args)
{
string output = "";
List<string> words = new List<string>();
using (StreamReader file = new StreamReader("Dictionary.txt"))
{
string line;
//Each line of this file is a new word
while ((line = file.ReadLine()) != null)
words.Add(line.Trim());
}
string input = Console.ReadLine();
int failed = 0;
while (words.Count > 0 || failed == words.Count)
{
remainingLetters = input;
foreach (string word in words.OrderByDescending(x => x.Length).Where(x => x.Length <= input.Replace(" ", "").Count()).ToList())
{
if(remainingLetters.Trim() == "")
break;
if (CheckWord(remainingLetters, word))
output = " " + word;
else
failed++;
words.Remove(word);
}
if(output.Trim().Length > 0 && remainingLetters.Trim().Length == 0)
{
Console.WriteLine(input + " -> " + output.Trim());
output = "";
}
}
}
/// <summary>
/// Determines if you can make the word given the letters
/// </summary>
/// <param name="letters">
/// A string of every letter you have
/// </param>
/// <param name="word">
/// Word you wish to create
/// </param>
static bool CheckWord(string letters, string word)
{
foreach (char c in word)
if (letters.Contains(c))
letters = letters.Remove(letters.IndexOf(c), 1);
else
return false;
remainingLetters = letters;
return true;
}
}
}
@Xmerr
Copy link
Author

Xmerr commented Dec 7, 2016

For Reddit Intermediate Challenge 280

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment